is there an nicer way for this expression?

Discussion in 'Ruby' started by Remco Hh, Nov 20, 2007.

  1. Remco Hh

    Remco Hh Guest

    hi,
    perhaps a stupid question.

    i do this a lot: if (foo.bar==1 or foo.bar==2)

    can i make this expression shorter and nicer?,
    something like if (foo.bar=1,2), which of course doesn't work :)

    regards,

    remco
    --
    Posted via http://www.ruby-forum.com/.
     
    Remco Hh, Nov 20, 2007
    #1
    1. Advertising

  2. Re: is there an nicer way for this expression?

    2007/11/20, Remco Hh <>:
    > hi,
    > perhaps a stupid question.
    >
    > i do this a lot: if (foo.bar==1 or foo.bar==2)
    >
    > can i make this expression shorter and nicer?,
    > something like if (foo.bar=1,2), which of course doesn't work :)


    # note: if you do this frequently you should probably
    # put the array in a constant to save the array
    # creation overhead
    if [1,2].include? foo ...

    or

    case foo
    when 1,2
    ...
    else
    end

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Nov 20, 2007
    #2
    1. Advertising

  3. Remco Hh wrote:
    > hi,
    > perhaps a stupid question.
    >
    > i do this a lot: if (foo.bar==1 or foo.bar==2)
    >
    > can i make this expression shorter and nicer?,
    > something like if (foo.bar=1,2), which of course doesn't work :)
    >
    > regards,
    >
    > remco


    foo.bar == 1 || 2
    --
    Posted via http://www.ruby-forum.com/.
     
    Pokkai Dokkai, Nov 20, 2007
    #3
  4. Re: is there an nicer way for this expression?

    2007/11/20, Pokkai Dokkai <>:
    > Remco Hh wrote:
    > > hi,
    > > perhaps a stupid question.
    > >
    > > i do this a lot: if (foo.bar==1 or foo.bar==2)
    > >
    > > can i make this expression shorter and nicer?,
    > > something like if (foo.bar=1,2), which of course doesn't work :)
    > >
    > > regards,
    > >
    > > remco

    >
    > foo.bar == 1 || 2


    Sure? Did you test this?

    $ ruby -e '5.times {|foo| p [foo, foo == 1 || 2]}'
    [0, 2]
    [1, true]
    [2, 2]
    [3, 2]
    [4, 2]

    All cases are non nil and non false => true.

    robert


    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Nov 20, 2007
    #4
  5. Pokkai Dokkai wrote:

    > foo.bar == 1 || 2


    sorry , i forgot
    try this
    foo.bar == (1||2)



    --
    Posted via http://www.ruby-forum.com/.
     
    Pokkai Dokkai, Nov 20, 2007
    #5
  6. Pokkai Dokkai wrote:
    > foo.bar == (1||2)


    1||2 is 1. Always.


    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Nov 20, 2007
    #6
  7. Remco Hh

    Robert Dober Guest

    Re: is there an nicer way for this expression?

    what about?
    (1..2) === foo.bar

    Be careful about the domain of foo.bar though, as
    (1..2) === 1.5 --> true

    Cheers
    Robert
     
    Robert Dober, Nov 20, 2007
    #7
  8. Remco Hh

    Brian Adkins Guest

    Re: is there an nicer way for this expression?

    On Nov 20, 7:38 am, Remco Hh <> wrote:
    > hi,
    > perhaps a stupid question.
    >
    > i do this a lot: if (foo.bar==1 or foo.bar==2)
    >
    > can i make this expression shorter and nicer?,
    > something like if (foo.bar=1,2), which of course doesn't work :)


    You could have:

    foo.bar.equals_one(1, 2, 7)

    or

    equals_one(foo.bar, 1, 2, 7)

    How often do you do this? Your original expression is probably more
    readable if you typically only have two values to compare. If you have
    three or more, a function *may* help (that's why I added a param to
    the above). Another option would be to create a macro in your editor
    to easily type the expression with minimal keystrokes.

    Brian Adkins
     
    Brian Adkins, Nov 20, 2007
    #8
  9. Remco Hh

    Brian Adkins Guest

    Re: is there an nicer way for this expression?

    On Nov 20, 8:42 am, Brian Adkins <> wrote:
    > On Nov 20, 7:38 am, Remco Hh <> wrote:
    >
    > > hi,
    > > perhaps a stupid question.

    >
    > > i do this a lot: if (foo.bar==1 or foo.bar==2)

    >
    > > can i make this expression shorter and nicer?,
    > > something like if (foo.bar=1,2), which of course doesn't work :)

    >
    > You could have:
    >
    > foo.bar.equals_one(1, 2, 7)
    >
    > or
    >
    > equals_one(foo.bar, 1, 2, 7)


    equals_any is probably a better name...
     
    Brian Adkins, Nov 20, 2007
    #9
  10. Remco Hh

    Robert Dober Guest

    Re: is there an nicer way for this expression?

    On Nov 20, 2007 2:50 PM, Brian Adkins <> wrote:
    > On Nov 20, 8:42 am, Brian Adkins <> wrote:
    > > On Nov 20, 7:38 am, Remco Hh <> wrote:
    > >
    > > > hi,
    > > > perhaps a stupid question.

    > >
    > > > i do this a lot: if (foo.bar==1 or foo.bar==2)

    > >
    > > > can i make this expression shorter and nicer?,
    > > > something like if (foo.bar=1,2), which of course doesn't work :)

    > >
    > > You could have:
    > >
    > > foo.bar.equals_one(1, 2, 7)
    > >
    > > or
    > >
    > > equals_one(foo.bar, 1, 2, 7)

    >
    > equals_any is probably a better name...
    >
    >

    class Object
    def one_of? *values
    if values.size == 1 && values.respond_to?( :include? ) then
    values.first.include? self
    else
    values.include? self
    end
    end
    end

    Do we need this? Nah.
    Do you need this? if so go ahead;)

    Cheers
    Robert


    --
    what do I think about Ruby?
    http://ruby-smalltalk.blogspot.com/
     
    Robert Dober, Nov 20, 2007
    #10
  11. Remco Hh

    Trans Guest

    Re: is there an nicer way for this expression?

    On Nov 20, 7:38 am, Remco Hh <> wrote:
    > hi,
    > perhaps a stupid question.
    >
    > i do this a lot: if (foo.bar==1 or foo.bar==2)
    >
    > can i make this expression shorter and nicer?,
    > something like if (foo.bar=1,2), which of course doesn't work :)


    One way:

    if [1,2].include?(foo.bar)
    # ...
    end

    another:

    case foo.bar when 1,2 then
    # ...
    end

    T.
     
    Trans, Nov 20, 2007
    #11
  12. Re: is there an nicer way for this expression?

    2007/11/20, Trans <>:
    >
    >
    > On Nov 20, 7:38 am, Remco Hh <> wrote:
    > > hi,
    > > perhaps a stupid question.
    > >
    > > i do this a lot: if (foo.bar==1 or foo.bar==2)
    > >
    > > can i make this expression shorter and nicer?,
    > > something like if (foo.bar=1,2), which of course doesn't work :)

    >
    > One way:
    >
    > if [1,2].include?(foo.bar)
    > # ...
    > end
    >
    > another:
    >
    > case foo.bar when 1,2 then
    > # ...
    > end


    Hey, this looks familiar! ;-)

    Cheers

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Nov 20, 2007
    #12
  13. Remco Hh

    Remco Hh Guest

    Re: is there an nicer way for this expression?

    thank a lot for the input!

    regards,
    Remco
    --
    Posted via http://www.ruby-forum.com/.
     
    Remco Hh, Nov 20, 2007
    #13
  14. Remco Hh

    Brian Adkins Guest

    Re: is there an nicer way for this expression?

    On Nov 20, 9:11 am, Robert Dober <> wrote:
    > class Object
    > def one_of? *values
    > ...
    > end
    > end


    Nice name choice :)
     
    Brian Adkins, Nov 20, 2007
    #14
  15. Remco Hh

    Peña, Botp Guest

    Re: is there an nicer way for this expression?

    From: Trans [mailto:]=20
    # case foo.bar when 1,2 then

    hmm ;-)

    bar
    #=3D> 1
    bar.in? 1,2
    #=3D> 0
    bar.in? 1,2,3
    #=3D> 0
    bar.in? 2,1,3
    #=3D> 1
    bar.in? *[2,1,3]
    #=3D> 1
    bar.in? [2,1,3],1,2
    #=3D> 1
    [2,1,3].in? [2,1,3],1,2
    #=3D> 0
    [2].in? [2,1,3],1,2
    #=3D> nil
    {1=3D>2}.in? [2,1,3],1,2,{1=3D>2},{2=3D>4}
    #=3D> 3
    bar.in? *(1..2)
    #=3D> 0
    bar.in? 3,4,*(1..2)
    #=3D> 2
    bar.in? 3,4,*(2..5)
    #=3D> nil

    aliased w among, amongst, and one_of (just lately)

    kind regards -botp
     
    Peña, Botp, Nov 21, 2007
    #15
  16. Remco Hh

    Brian Adkins Guest

    Re: is there an nicer way for this expression?

    On Nov 20, 10:34 pm, Peña, Botp <> wrote:
    > From: Trans [mailto:]
    > # case foo.bar when 1,2 then
    >
    > hmm ;-)
    >
    > bar
    > #=> 1
    > bar.in? 1,2
    > #=> 0
    > bar.in? 1,2,3
    > #=> 0
    > bar.in? 2,1,3
    > #=> 1
    > bar.in? *[2,1,3]
    > #=> 1
    > bar.in? [2,1,3],1,2
    > #=> 1
    > [2,1,3].in? [2,1,3],1,2
    > #=> 0
    > [2].in? [2,1,3],1,2
    > #=> nil
    > {1=>2}.in? [2,1,3],1,2,{1=>2},{2=>4}
    > #=> 3
    > bar.in? *(1..2)
    > #=> 0
    > bar.in? 3,4,*(1..2)
    > #=> 2
    > bar.in? 3,4,*(2..5)
    > #=> nil
    >
    > aliased w among, amongst, and one_of (just lately)
    >
    > kind regards -botp


    class Object
    def equals_any *args
    args.each {|x| return true if self == x }
    return false
    end
    end
     
    Brian Adkins, Nov 21, 2007
    #16
  17. Remco Hh

    Wayne Magor Guest

    Remco Hh wrote:
    > hi,
    > perhaps a stupid question.
    >
    > i do this a lot: if (foo.bar==1 or foo.bar==2)
    >
    > can i make this expression shorter and nicer?,
    > something like if (foo.bar=1,2), which of course doesn't work :)
    >
    > regards,
    >
    > remco


    Not a stupid question at all. Many people want this in Ruby!

    Here's the solution (which should be standard in Ruby):

    class Object
    def in?(an_array)
    an_array.include?(self)
    end
    end


    Then, your desire to have something like (foo.bar=1,2) would become:

    foo.bar.in?([1,2])
    --
    Posted via http://www.ruby-forum.com/.
     
    Wayne Magor, Nov 21, 2007
    #17
  18. Re: is there an nicer way for this expression?

    On Nov 21, 2007 10:35 AM, Wayne Magor <> wrote:
    > Not a stupid question at all. Many people want this in Ruby!
    >
    > Here's the solution (which should be standard in Ruby):
    >
    > class Object
    > def in?(an_array)
    > an_array.include?(self)
    > end
    > end
    >
    >
    > Then, your desire to have something like (foo.bar=1,2) would become:
    >
    > foo.bar.in?([1,2])
    >


    Yeah, The Ruby Way also says that this should be part of the language.
    But I'm not sure it makes sense from an OO perspective - being "in" a
    container (or having a copy of oneself in that container) is not a
    property of an object, but rather a property of the container. It's
    convenient, but seems logically backwards.
     
    Jesse Edelstein, Nov 21, 2007
    #18
    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. Markus Rosenstihl

    Nicer way of strip and replace?

    Markus Rosenstihl, Oct 11, 2005, in forum: Python
    Replies:
    6
    Views:
    387
    Markus Rosenstihl
    Oct 12, 2005
  2. Stefan Arentz

    Is there a nicer way to do this?

    Stefan Arentz, Oct 4, 2007, in forum: Python
    Replies:
    12
    Views:
    459
    Victor B. Gonzalez
    Oct 5, 2007
  3. News123
    Replies:
    3
    Views:
    3,539
    Paul McGuire
    Jul 14, 2010
  4. S Kanakakorn
    Replies:
    3
    Views:
    388
    Jules
    Feb 9, 2007
  5. Chris Smith

    Newbie Q - Nicer way to lc something?

    Chris Smith, Oct 1, 2003, in forum: Perl Misc
    Replies:
    16
    Views:
    173
    Chris Smith
    Oct 4, 2003
Loading...

Share This Page