|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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/. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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/. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Pokkai Dokkai wrote:
> foo.bar == 1 || 2 sorry , i forgot try this foo.bar == (1||2) -- Posted via http://www.ruby-forum.com/. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Pokkai Dokkai wrote:
> foo.bar == (1||2) 1||2 is 1. Always. -- Jabber: sepp2k@jabber.org ICQ: 205544826 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
what about?
(1..2) === foo.bar Be careful about the domain of foo.bar though, as (1..2) === 1.5 --> true Cheers Robert |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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* (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 |
|
![]() |
| Outils de la discussion | |
|
|