Chris Bunch Chris Bunch - 3 months ago 15
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 = "0.0.0.0"
goo = "here is some other stuff 0.0.0.0"
if goo =~ /value of foo here dynamically/
puts "success!"
end

Answer Source

Same as string insertion.

if goo =~ /#{Regexp.quote(foo)}/
#...