PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > is there an nicer way for this expression?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
is there an nicer way for this expression?

Réponse
 
LinkBack Outils de la discussion
Vieux 20/11/2007, 12h38   #1
Remco Hh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

regards,

remco
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 20/11/2007, 12h41   #2
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: is there an nicer way for this expression?

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

  Réponse avec citation
Vieux 20/11/2007, 12h44   #3
Pokkai Dokkai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: is there an nicer way for this expression?

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

  Réponse avec citation
Vieux 20/11/2007, 12h47   #4
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: is there an nicer way for this expression?

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

  Réponse avec citation
Vieux 20/11/2007, 12h48   #5
Pokkai Dokkai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: is there an nicer way for this expression?

Pokkai Dokkai wrote:

> foo.bar == 1 || 2


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



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

  Réponse avec citation
Vieux 20/11/2007, 12h54   #6
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: is there an nicer way for this expression?

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


1||2 is 1. Always.


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

  Réponse avec citation
Vieux 20/11/2007, 13h05   #7
Robert Dober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 20/11/2007, 13h42   #8
Brian Adkins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: is there an nicer way for this expression?

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h34.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10541 seconds with 16 queries