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 > newbie question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
newbie question

Réponse
 
LinkBack Outils de la discussion
Vieux 10/03/2008, 11h38   #1
Frantisek Psotka
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut newbie question

is in ruby operator for:

a = b if b

a (operator) b

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

  Réponse avec citation
Vieux 10/03/2008, 11h50   #2
Farrel Lifson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie question

On 10/03/2008, Frantisek Psotka <frantisek.psotka@matfyz.cz> wrote:
> is in ruby operator for:
>
> a = b if b
>
> a (operator) b


a ||= b

Farrel

  Réponse avec citation
Vieux 10/03/2008, 12h02   #3
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie question

Farrel Lifson wrote:
> On 10/03/2008, Frantisek Psotka <frantisek.psotka@matfyz.cz> wrote:


> > a = b if b

>
> a ||= b


Nope.
a ||= b is equivalent to a = a || b is equivalent to
a = if a then a else b end is NOT equivalent to a = b if b


HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

  Réponse avec citation
Vieux 10/03/2008, 12h03   #4
Thomas Wieczorek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie question

On Mon, Mar 10, 2008 at 11:50 AM, Farrel Lifson <farrel.lifson@gmail.com> wrote:
>
> a ||= b
>


That's not right. It will give you a NameError, that b is undefined.
a ||= b
is equivalent to
a = a || b
which is the same as
a = b if not a

>
> a = b if b
>


I know of no operator for it yet.

  Réponse avec citation
Vieux 10/03/2008, 12h04   #5
Jano Svitok
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie question

On Mon, Mar 10, 2008 at 11:50 AM, Farrel Lifson <farrel.lifson@gmail.com> wrote:
> On 10/03/2008, Frantisek Psotka <frantisek.psotka@matfyz.cz> wrote:
> > is in ruby operator for:
> >
> > a = b if b
> >
> > a (operator) b

>
> a ||= b
>
> Farrel


a ||= b is

a = a || b
that is
a = b if !a
that is
a = b unless a

It is equal to "a = b if b" if both a and b are of boolean type
(doesn't work for e.g. numbers).

  Réponse avec citation
Vieux 10/03/2008, 12h08   #6
Alex Young
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie question

On Mon, 2008-03-10 at 19:50 +0900, Farrel Lifson wrote:
> On 10/03/2008, Frantisek Psotka <frantisek.psotka@matfyz.cz> wrote:
> > is in ruby operator for:
> >
> > a = b if b
> >
> > a (operator) b

>
> a ||= b


Not quite:

irb(main):007:0> a = 1
=> 1
irb(main):008:0> b = 2
=> 2
irb(main):009:0> a ||= b
=> 1
irb(main):010:0>
irb(main):011:0* a
=> 1
irb(main):012:0> a = b if b
=> 2
irb(main):013:0> a
=> 2

a ||= b is equivalent to a = b if !a, which isn't quite the same thing
as a = b if b.

--
Alex


  Réponse avec citation
Vieux 10/03/2008, 12h08   #7
Lars
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie question

On Mar 10, 11:50 am, Farrel Lifson <farrel.lif...@gmail.com> wrote:
> On 10/03/2008, Frantisek Psotka <frantisek.pso...@matfyz.cz> wrote:
>
> > is in ruby operator for:

>
> > a = b if b

>
> > a (operator) b

>
> a ||= b


"a ||= b" means "a = b unless a".

I don't think there is an operator for "a = b if b".

Luckily, "a = b if b" is valid ruby code. "a = b || a" or "a = (b or
a)" would also work.
  Réponse avec citation
Vieux 10/03/2008, 15h11   #8
Frantisek Psotka
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie question

yes, i can write

a = b || a

but isn't it more complex. imagine that b is nil. then a = a will be
evaluated?

maybe time for feature request? new operator a =|| (a = b if b)

(variable = params[:nice_symbol] if params[:nice_symbol] doesnt look
very nice)


Lars wrote:
> On Mar 10, 11:50 am, Farrel Lifson <farrel.lif...@gmail.com> wrote:
>> On 10/03/2008, Frantisek Psotka <frantisek.pso...@matfyz.cz> wrote:
>>
>> > is in ruby operator for:

>>
>> > a = b if b

>>
>> > a (operator) b

>>
>> a ||= b

>
> "a ||= b" means "a = b unless a".
>
> I don't think there is an operator for "a = b if b".
>
> Luckily, "a = b if b" is valid ruby code. "a = b || a" or "a = (b or
> a)" would also work.


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

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


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13078 seconds with 16 queries