is there an nicer way for this expression?

R

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

regards,

remco
 
R

Robert Klemme

2007/11/20 said:
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
 
P

Pokkai Dokkai

Remco said:
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
 
R

Robert Klemme

2007/11/20 said:
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
 
R

Robert Dober

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

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

Cheers
Robert
 
B

Brian Adkins

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
 
R

Robert Dober

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
 
T

Trans

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

Robert Klemme

2007/11/20 said:
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
 
P

Peña, Botp

From: Trans [mailto:[email protected]]=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
 
B

Brian Adkins

From: Trans [mailto:[email protected]]
# 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
 
W

Wayne Magor

Remco said:
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])
 
J

Jesse Edelstein

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top