From: Robert Klemme Date: 2008-11-11T16:22:37+09:00 Subject: Re: Comparing variable to multiple values On 11.11.2008 01:56, Brian Adkins wrote: > Greg Lazarev writes: > >> I'm wondering if there's a better way to do this in ruby: >> >> if a == "word1" || a == "word2" or || a == "word3" >> puts "good" >> end > > I realize that you probably want a more general solution (which others > have already provided); however, if 'a' *does* follow a pattern: > > puts 'good' if a =~ /word[1-3]/ > > or > > puts 'good' if a =~ /word\d+/ You forgot the anchors. Your regexp will also match "fooword1bar" which was not intended by OP. Also, IIRC it is more efficient to switch sides, i.e. puts 'good' if /\Aword[1-3]\z/ =~ a For _large_ sets of words which do not follow a simple pattern a Set may be more efficient TEST = %w{word1 word2 word3 plus many more words}.to_set.freeze puts 'good' if TEST.include? a Kind regards robert