Is there an nicer way for this expression?

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 :slight_smile:

regards,

remco

2007/11/20, Remco Hh [email protected]:

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 :slight_smile:

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

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 :slight_smile:

regards,

remco

foo.bar == 1 || 2

Pokkai D. wrote:

foo.bar == 1 || 2

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

Pokkai D. wrote:

foo.bar == (1||2)

1||2 is 1. Always.

2007/11/20, Pokkai D. [email protected]:

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

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

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

Cheers
Robert

On Nov 20, 8:42 am, Brian A. [email protected] wrote:

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…

On Nov 20, 7:38 am, Remco Hh [email protected] 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 :slight_smile:

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

On Nov 20, 2007 2:50 PM, Brian A. [email protected] wrote:

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

On Nov 20, 7:38 am, Remco Hh [email protected] 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 :slight_smile:

One way:

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

another:

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

T.

2007/11/20, Trans [email protected]:

end
Hey, this looks familiar! :wink:

Cheers

robert

thank a lot for the input!

regards,
Remco

Hi,

On Tue, 2007-11-20 at 21:48 +0900, Pokkai D. wrote:

Pokkai D. wrote:

foo.bar == 1 || 2

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

No, that won’t work either.

irb(main):006:0> 1||2
=> 1
irb(main):007:0> 2||1
=> 2
irb(main):008:0> false || 1
=> 1
irb(main):009:0> true || 1
=> true
irb(main):010:0> nil || 3
=> 3
irb(main):011:0>

Evaluate it one step at a time to see what you’re doing.

Cheers,

Arlen

From: Trans [mailto:[email protected]]

case foo.bar when 1,2 then

hmm :wink:

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

On Nov 20, 9:11 am, Robert D. [email protected] wrote:

class Object
def one_of? *values

end
end

Nice name choice :slight_smile:

On Nov 20, 10:34 pm, Peña, Botp [email protected] wrote:

#=> 0
{1=>2}.in? [2,1,3],1,2,{1=>2},{2=>4}
kind regards -botp
class Object
def equals_any *args
args.each {|x| return true if self == x }
return false
end
end

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 :slight_smile:

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

On Nov 21, 2007 10:35 AM, Wayne M. [email protected] wrote:

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.