views:

81

answers:

4

I want to do something like

assert_match /blah blah blah #{@user}/, @some_text

but I'm not having any luck with it working.

What am I doing wrong here?

+1  A: 

That is the correct way to insert a variable into a regex:

irb(main):001:0> a='Hi'
=> "Hi"
irb(main):002:0> b=/Not #{a}/
=> /Not Hi/

So your problem is likely that the assert is failing because of a bad match. Check the value of @user and @some_text and try http://rubular.com to come up with a matching regexp

Drew Blas
+1  A: 

assert_match also takes a string, like

assert_match("blah blah", 'a string')

so the other way to do what you are doing would be to user string interpolation

string_to_match = "blah blah #{@user}"

assert_match(string_to_match, 'a string')
nas
+2  A: 

If there's any possibility of the @user string containing special regexp characters, you should modify this to:

/blah blah blah #{Regexp.escape(@user)}/
glenn mcdonald
+1  A: 

you can take a look at %r{}, for example:

pattern = 'foo'
%r{#{pattern}_bar} =~ 'foo_bar_2000'

leomayleomay