Hiya
How do I get a variable into a regexp?
In version 1.6.8 I could do it - but version 1.8 complains about using
an explicit regexp:
var = "apple"
array = %w(pineapple, hamapple, cheese, dog, cat, apple)
array.each do |x|
if x =~ var then puts x
end
end
I have tried doing if x =~ /var/ and =~ /#{var}/ but that doesnt work
Any ideas?
Thanks
Kingsley
In version 1.6.8 I could do it - but version 1.8 complains about using
an explicit regexp:
var = “apple”
array = %w(pineapple, hamapple, cheese, dog, cat, apple)
array.each do |x|
if x =~ var then puts x
end
end
That’s because you’re using =~ to match a string to a string, rather than a
string to a regexp.
I have tried doing if x =~ /var/ and =~ /#{var}/ but that doesnt work
The second of those should be fine:
var = “apple”
array = %w(pineapple hamapple cheese dog cat apple)
array.each do |x|
puts x if x =~ /#{var}/
end
#>> pineapple
hamapple
apple
Regards,
Brian.
···
On Thu, May 29, 2003 at 10:16:41PM +0900, Kingsley wrote: