user2014905 user2014905 - 2 months ago 21
Ruby Question

Rspec forward and back slash

I saw this line of code in betterspecs.

it { expect(assigns('message')).to match /it was born in Belville/ }


What does the slashes before 'it' and after 'Belville' mean?

Answer

The slashes indicate a Regexp-literal definition. It holds a regular expression, that can be matched againts other strings.

The example checks that the assigned instance variable message matches the regex it was born in Belville. In this scenario, it essentially just checks that message contains it was born in Belville. Regexes can get much more complicated than this, though.