⇤ home

Using variables in Ruby Regular Expressions

Posted 02 Sep 2009 in Programming and Ruby

Quick tip on howto use variables in ruby with regular expressions:

re = "string"
"my string" =~ /#{re}/    # => 3

So you can do:

a = 'dog'
b = 'hates cats'
str = "my dog hates cats"
str.match(/#{a} #{b}/)    # => true

(posted for me to remember)

Related Posts