Is it possible to pass a string to a method in ruby and use this method to interpolate this string?
I have something like this:
do_a_search("location = #{location}")
...
def do_a_search(search_string)
location = ....
Model.where(search_string)
end
Context is RoR, but it is a common ruby question that I think. I understand that the above example seems a bit confusing, but I'm trying to reorganize a bunch of very repetitive methods.
The problem is that if I put the string in interpolation in double quotes, the location does not exist when the method is called, if I put it in single quotes, it will never be interpolated ...
What I really want to do is put it in single quotes and interpolate it later. I don’t think it is possible, or am I missing something?
edit: to be clear (as I think I have simplified what I'm trying to do above), one of the problems is that I can name this method in several contexts; Maybe I really want to call
do_a_search("country = #{country}")
or even
do_a_search("country = #{country} AND location = #{location})
(the country also exists as a local var in my method). Therefore, I want to pass everything necessary for replacement in the method call
I think the facets gem String.interpolate method will solve my problem, but it does not work in rails 4
source
share