Comparing variable to multiple values

Discussion in 'Ruby' started by Greg Lazarev, Nov 11, 2008.

  1. Greg Lazarev

    Greg Lazarev Guest

    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, Nov 11, 2008
    #1
    1. Advertising

  2. Greg Lazarev

    Alex Fenton Guest

    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
    Alex Fenton, Nov 11, 2008
    #2
    1. Advertising

  3. [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)
    Victor H. Goff III, Nov 11, 2008
    #3
  4. Greg Lazarev

    Brian Adkins Guest

    Greg Lazarev <> 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/
    http://lojic.com/blog/
    Brian Adkins, Nov 11, 2008
    #4
  5. RnJvbTogR3JlZyBMYXphcmV2IFttYWlsdG86cnVzc2lhbmJhbmRpdEBnbWFpbC5jb21dIA0KIyBJ
    J20gd29uZGVyaW5nIGlmIHRoZXJlJ3MgYSBiZXR0ZXIgd2F5IHRvIGRvIHRoaXMgaW4gcnVieToN
    CiMgaWYgYSA9PSAid29yZDEiIHx8IGEgPT0gIndvcmQyIiBvciB8fCBhID09ICJ3b3JkMyINCiMg
    ICBwdXRzICJnb29kIg0KIyBlbmQNCg0KcnVieSBhbGxvd3MgeW91IHRvIGJlYXV0aWZ5IDspDQoN
    Cj4gZGVmIGluPyBjb250YWluZXINCj4gICBjb250YWluZXIuaW5jbHVkZT8gc2VsZg0KPiBlbmQN
    Cj0+IG5pbA0KDQo+IHB1dHMgImdvb2QiIGlmIGEuaW4/ICV3KHdvcmQxIHdvcmQyIHdvcmQzIHdo
    YXRldmVyKQ0KZ29vZA0KDQpmd2l3LCBzb21ldGltZXMgaW4gY2FzZXMgbGlrZSB0aGlzLCBpIHN0
    YXJ0IG9mZiB3aXRoIGEuLi4gY2FzZSwNCg0KPiBjYXNlIGENCj4gd2hlbiAid29yZDEiLCAid29y
    ZDIiLCJ3b3JkMyINCj4gICBwdXRzICJnb29kIg0KPiBlbmQNCg0KdGhlbiBqdXN0IHNwcmlua2xl
    IHcgbW9yZSBjYXNlcywgYW5kICBiZWF1dGlmeSBhcyBpIGdvLi4uDQo=
    Peña, Botp, Nov 11, 2008
    #5
  6. On 11.11.2008 01:56, Brian Adkins wrote:
    > Greg Lazarev <> 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
    Robert Klemme, Nov 11, 2008
    #6
  7. 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-forum.com/.
    Siep Korteling, Nov 11, 2008
    #7
  8. RnJvbTogU2llcCBLb3J0ZWxpbmcgW21haWx0bzpzLmtvcnRlbGluZ0BnbWFpbC5jb21dIA0KIyBw
    dXRzICJnb29kIiBpZiAoIndvcmQxIi4uIndvcmQzIikuaW5jbHVkZT8oIndvcmQxYmFyIikNCiMg
    PT4gZ29vZA0KIyBJZiBzbywgd2hhdCBhbSBJIHN1cHBvc2VkIHRvIGRvPw0KDQpydWJ5IHJhbmdl
    IGlzIGp1c3QgYmVpbmcgZnJpZW5kbHksIHNvIGRvIG5vdCByZWx5IHRvbyBtdWNoIDspDQp0byBi
    ZSBleHBsaWNpdCwgY29udmVydCBpdCB0byBhcnJheSB0aGVuDQoNCj4gcHV0cyAiZ29vZCIgaWYg
    KCJ3b3JkMSIuLiJ3b3JkMyIpLnRvX2EuaW5jbHVkZT8oIndvcmQxYmFyIikNCj0+IG5pbA0K
    Peña, Botp, Nov 11, 2008
    #8
  9. Greg Lazarev

    Brian Adkins Guest

    Robert Klemme <> 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/
    http://lojic.com/blog/
    Brian Adkins, Nov 11, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jorgen Gustafsson
    Replies:
    4
    Views:
    573
    Jorgen Gustafsson
    Dec 12, 2003
  2. darrel
    Replies:
    2
    Views:
    5,054
    darrel
    Dec 23, 2005
  3. Chaz
    Replies:
    2
    Views:
    634
  4. Kristof Bastiaensen

    RCR: Comparing multiple values

    Kristof Bastiaensen, Apr 25, 2004, in forum: Ruby
    Replies:
    5
    Views:
    101
    Kristof Bastiaensen
    Apr 26, 2004
  5. Jason
    Replies:
    8
    Views:
    131
    Dr.Ruud
    Jul 27, 2006
Loading...

Share This Page