Comparing variable to multiple values


#1

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


#2

Greg L. wrote:

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

if [“word1”, “word2”, “word3”].include?(a)
puts ‘good’
end

OR

case a
when “word1”, “word2”, “word3”
puts ‘good’
end

hth
a


#3

strings_array = [‘word1’, ‘word2’, ‘word3’]
a = ‘word1’
puts “good” if strings_array.include?(a)


#4

Greg L. removed_email_address@domain.invalid 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+/


#5

From: Greg L. [mailto:removed_email_address@domain.invalid]

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

ruby allows you to beautify :wink:

def in? container
container.include? self
end
=> nil

puts “good” if a.in? %w(word1 word2 word3 whatever)
good

fwiw, sometimes in cases like this, i start off with a… case,

case a
when “word1”, “word2”,“word3”
puts “good”
end

then just sprinkle w more cases, and beautify as i go…


#6

Robert K. wrote:

On 11.11.2008 01:56, Brian A. wrote:

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.
(…)

Kind regards

robert

Is this a bug?

(“word1”…“word3”).each{|w| puts w}
=> word1
=> word2
=> word3
puts “good” if (“word1”…“word3”).include?(“word1bar”)
=> good

If so, what am I supposed to do?

Siep


#7

On 11.11.2008 01:56, Brian A. wrote:

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


#8

From: Siep K. [mailto:removed_email_address@domain.invalid]

puts “good” if (“word1”…“word3”).include?(“word1bar”)

=> good

If so, what am I supposed to do?

ruby range is just being friendly, so do not rely too much :wink:
to be explicit, convert it to array then

puts “good” if (“word1”…“word3”).to_a.include?(“word1bar”)
=> nil


#9

Robert K. removed_email_address@domain.invalid writes:

On 11.11.2008 01:56, Brian A. wrote:

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.

Interesting - I wouldn’t think there would be a difference, but I just
benchmarked it and it appears that putting the pattern on the left
gives a 1.2% speed boost.