Chris Bunch Chris Bunch -3 years ago 95
Ruby Question

Interpolating a string into a regex

I need to substitute the value of a string into my regular expression in Ruby. Is there an easy way to do this? For example:

foo = ""
goo = "here is some other stuff"
if goo =~ /value of foo here dynamically/
puts "success!"

Answer Source

Same as string insertion.

if goo =~ /#{Regexp.quote(foo)}/
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download