[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

is there an nicer way for this expression?

Remco Hh

11/20/2007 12:38:00 PM

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

18 Answers

Robert Klemme

11/20/2007 12:41:00 PM

0

2007/11/20, Remco Hh <remco@huijdts.nl>:
> 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

Pokkai Dokkai

11/20/2007 12:44:00 PM

0

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

Robert Klemme

11/20/2007 12:48:00 PM

0

2007/11/20, Pokkai Dokkai <bad_good_lion@yahoo.com>:
> 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

Pokkai Dokkai

11/20/2007 12:48:00 PM

0

Pokkai Dokkai wrote:

> foo.bar == 1 || 2

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



--
Posted via http://www.ruby-....

Sebastian Hungerecker

11/20/2007 12:54:00 PM

0

Pokkai Dokkai wrote:
> foo.bar == (1||2)

1||2 is 1. Always.


--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Robert Dober

11/20/2007 1:05:00 PM

0

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

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

Cheers
Robert

Brian Adkins

11/20/2007 1:43:00 PM

0

On Nov 20, 7:38 am, Remco Hh <re...@huijdts.nl> 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

11/20/2007 1:49:00 PM

0

On Nov 20, 8:42 am, Brian Adkins <lojicdot...@gmail.com> wrote:
> On Nov 20, 7:38 am, Remco Hh <re...@huijdts.nl> 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...

Robert Dober

11/20/2007 2:11:00 PM

0

On Nov 20, 2007 2:50 PM, Brian Adkins <lojicdotcom@gmail.com> wrote:
> On Nov 20, 8:42 am, Brian Adkins <lojicdot...@gmail.com> wrote:
> > On Nov 20, 7:38 am, Remco Hh <re...@huijdts.nl> 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.blo...

Trans

11/20/2007 2:24:00 PM

0



On Nov 20, 7:38 am, Remco Hh <re...@huijdts.nl> 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.