Comparing variable to multiple values

G

Greg Lazarev

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
 
A

Alex Fenton

Greg said:
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
 
V

Victor H. Goff III

[Note: parts of this message were removed to make it a legal post.]

strings_array = ['word1', 'word2', 'word3']
a = 'word1'
puts "good" if strings_array.include?(a)
 
B

Brian Adkins

Greg Lazarev said:
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+/
 
P

Peña, Botp

RnJvbTogR3JlZyBMYXphcmV2IFttYWlsdG86cnVzc2lhbmJhbmRpdEBnbWFpbC5jb21dIA0KIyBJ
J20gd29uZGVyaW5nIGlmIHRoZXJlJ3MgYSBiZXR0ZXIgd2F5IHRvIGRvIHRoaXMgaW4gcnVieToN
CiMgaWYgYSA9PSAid29yZDEiIHx8IGEgPT0gIndvcmQyIiBvciB8fCBhID09ICJ3b3JkMyINCiMg
ICBwdXRzICJnb29kIg0KIyBlbmQNCg0KcnVieSBhbGxvd3MgeW91IHRvIGJlYXV0aWZ5IDspDQoN
Cj4gZGVmIGluPyBjb250YWluZXINCj4gICBjb250YWluZXIuaW5jbHVkZT8gc2VsZg0KPiBlbmQN
Cj0+IG5pbA0KDQo+IHB1dHMgImdvb2QiIGlmIGEuaW4/ICV3KHdvcmQxIHdvcmQyIHdvcmQzIHdo
YXRldmVyKQ0KZ29vZA0KDQpmd2l3LCBzb21ldGltZXMgaW4gY2FzZXMgbGlrZSB0aGlzLCBpIHN0
YXJ0IG9mZiB3aXRoIGEuLi4gY2FzZSwNCg0KPiBjYXNlIGENCj4gd2hlbiAid29yZDEiLCAid29y
ZDIiLCJ3b3JkMyINCj4gICBwdXRzICJnb29kIg0KPiBlbmQNCg0KdGhlbiBqdXN0IHNwcmlua2xl
IHcgbW9yZSBjYXNlcywgYW5kICBiZWF1dGlmeSBhcyBpIGdvLi4uDQo=
 
R

Robert Klemme

Greg Lazarev said:
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
 
S

Siep Korteling

Robert said:
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
 
P

Peña, Botp

RnJvbTogU2llcCBLb3J0ZWxpbmcgW21haWx0bzpzLmtvcnRlbGluZ0BnbWFpbC5jb21dIA0KIyBw
dXRzICJnb29kIiBpZiAoIndvcmQxIi4uIndvcmQzIikuaW5jbHVkZT8oIndvcmQxYmFyIikNCiMg
PT4gZ29vZA0KIyBJZiBzbywgd2hhdCBhbSBJIHN1cHBvc2VkIHRvIGRvPw0KDQpydWJ5IHJhbmdl
IGlzIGp1c3QgYmVpbmcgZnJpZW5kbHksIHNvIGRvIG5vdCByZWx5IHRvbyBtdWNoIDspDQp0byBi
ZSBleHBsaWNpdCwgY29udmVydCBpdCB0byBhcnJheSB0aGVuDQoNCj4gcHV0cyAiZ29vZCIgaWYg
KCJ3b3JkMSIuLiJ3b3JkMyIpLnRvX2EuaW5jbHVkZT8oIndvcmQxYmFyIikNCj0+IG5pbA0K
 
B

Brian Adkins

Robert Klemme said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top