From: Brian Candler Date: 2003-05-29T23:14:14+09:00 Subject: Re: getting a variable into regexp problems On Thu, May 29, 2003 at 10:16:41PM +0900, Kingsley wrote: > 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.