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’m wondering if there’s a better way to do this in ruby:
if a == “word1” || a == “word2” or || a == “word3”
puts “good”
end
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
strings_array = [‘word1’, ‘word2’, ‘word3’]
a = ‘word1’
puts “good” if strings_array.include?(a)
Greg L. [email protected] 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+/
From: Greg L. [mailto:[email protected]]
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. 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
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
From: Siep K. [mailto:[email protected]]
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
Robert K. [email protected] 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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs