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
···
--
Posted via http://www.ruby-forum.com/.
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
--
Posted via http://www.ruby-forum.com/.
Greg Lazarev 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 Lazarev <russianbandit@gmail.com> 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+/
--
Brian Adkins
http://www.lojic.com/
# 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...
From: Greg Lazarev [mailto:russianbandit@gmail.com]
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
On 11.11.2008 01:56, Brian Adkins wrote:
Greg Lazarev <russianbandit@gmail.com> 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"
endI 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+/
Robert Klemme 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
Robert Klemme <shortcutter@googlemail.com> writes:
On 11.11.2008 01:56, Brian Adkins 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.
--
Brian Adkins
http://www.lojic.com/
# 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
From: Siep Korteling [mailto:s.korteling@gmail.com]