[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Comparing variable to multiple values

Greg Lazarev

11/11/2008 12:26:00 AM

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-....

8 Answers

Alex Fenton

11/11/2008 12:41:00 AM

0

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

Victor H. Goff III

11/11/2008 12:46:00 AM

0

[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)

Brian Adkins

11/11/2008 12:57:00 AM

0

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....
http://lojic...

Peña, Botp

11/11/2008 1:47:00 AM

0

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

Robert Klemme

11/11/2008 7:25:00 AM

0

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"
>> 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

Siep Korteling

11/11/2008 8:26:00 AM

0

Robert Klemme wrote:
> 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.
(...)
>
> 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
--
Posted via http://www.ruby-....

Peña, Botp

11/11/2008 8:51:00 AM

0

RnJvbTogU2llcCBLb3J0ZWxpbmcgW21haWx0bzpzLmtvcnRlbGluZ0BnbWFpbC5jb21dIA0KIyBw
dXRzICJnb29kIiBpZiAoIndvcmQxIi4uIndvcmQzIikuaW5jbHVkZT8oIndvcmQxYmFyIikNCiMg
PT4gZ29vZA0KIyBJZiBzbywgd2hhdCBhbSBJIHN1cHBvc2VkIHRvIGRvPw0KDQpydWJ5IHJhbmdl
IGlzIGp1c3QgYmVpbmcgZnJpZW5kbHksIHNvIGRvIG5vdCByZWx5IHRvbyBtdWNoIDspDQp0byBi
ZSBleHBsaWNpdCwgY29udmVydCBpdCB0byBhcnJheSB0aGVuDQoNCj4gcHV0cyAiZ29vZCIgaWYg
KCJ3b3JkMSIuLiJ3b3JkMyIpLnRvX2EuaW5jbHVkZT8oIndvcmQxYmFyIikNCj0+IG5pbA0K

Brian Adkins

11/11/2008 5:47:00 PM

0

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....
http://lojic...