Issue
I'm running the grep method to filter by pattern matching. This is an example code.
companies.grep /city/
However, ruby isn't allowing me to input the area_code
withing the block inside the rails view. Instead, I'm forced to hardcode it like so:
companies.grep /miami/
Keep in mind, city is a variable. For example,
city = miami
However, it updates. Do you know how can I pass a variable through the grep method?
Also, I tried companies.grep /#{city}/
, but it didn't work
Solution
companies.grep /#{city}/
# or
companies.grep Regexp.new(city)
In case of simple alpha-numeric query, this should suffice; but it is better to get into practice of escaping those, if you don't intend to have regexp operators. Thus, better version:
companies.grep /#{Regexp.escape city}/
# or
companies.grep Regexp.new(Regexp.escape city)
Answered By - Amadan