Forum: Ruby Comparing variable to multiple values

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Greg L. (Guest)
on 2008-11-11 02:28
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
Alex F. (Guest)
on 2008-11-11 02:45
(Received via mailing list)
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
Victor G. (Guest)
on 2008-11-11 02:49
(Received via mailing list)
strings_array = ['word1', 'word2', 'word3']
a = 'word1'
puts "good" if strings_array.include?(a)
Brian A. (Guest)
on 2008-11-11 03:00
(Received via mailing list)
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+/
Peña, Botp (Guest)
on 2008-11-11 03:49
(Received via mailing list)
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 ;)

> 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...
Robert K. (Guest)
on 2008-11-11 09:25
(Received via mailing list)
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
Siep K. (Guest)
on 2008-11-11 10:28
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
Peña, Botp (Guest)
on 2008-11-11 10:54
(Received via mailing list)
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 ;)
to be explicit, convert it to array then

> puts "good" if ("word1".."word3").to_a.include?("word1bar")
=> nil
Brian A. (Guest)
on 2008-11-11 19:50
(Received via mailing list)
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.
This topic is locked and can not be replied to.