How to use ruby variables inside regular expressions
In this case it is best to learn by example, fire up irb and enter:
str = "this string has ruby in it"
re = 'ruby'
str =~ /#{re}/
The 16 that gets returned is the location of the first matching character, ‘r’. You can also use this with gsub, for instance:
str = "uhhhh, I don't have a good example"
speech_correcting_reg_ex = 'uh+,?\s?'
str.gsub!(/#{speech_correcting_reg_ex}/,'')
And just like that, we have proper English. Should there be a comma there?
Thanks for reading, now find something to do at GoLark.com
1 Comment