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

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 11h58   #1
Fernando Cacciola
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut operator |=

Hi people,

Did I dream about an opeartor |=?

I can't find any reference to it in the online docs, yet I think I've seen
it in some samples.

What does it do?

TIA


--
Fernando Cacciola
SciSoft
http://fcacciola.50webs.com



  Réponse avec citation
Vieux 06/11/2007, 12h21   #2
Eustáquio 'TaQ' Rangel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operator |=

> Did I dream about an opeartor |=?
> I can't find any reference to it in the online docs, yet I think I've
> seen it in some samples.
> What does it do?


It sets a value if the variable does not have one, like:

>> foo ||= :bar

=> :bar
>> foo ||= :xyz

=> :bar

Regards,

--
Eustáquio "TaQ" Rangel
http://eustaquiorangel.com

"Simplicity is the ultimate sophistication."
Leonardo da Vinci

  Réponse avec citation
Vieux 06/11/2007, 12h31   #3
John Joyce
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operator |=


On Nov 6, 2007, at 5:58 AM, Fernando Cacciola wrote:

> Hi people,
>
> Did I dream about an opeartor |=?
>
> I can't find any reference to it in the online docs, yet I think
> I've seen it in some samples.
>
> What does it do?
>
> TIA
>

It's not exactly an operator itself, it is syntactic sugar.
You can find it well discussed in the archives.
|= means David A. Black.

  Réponse avec citation
Vieux 06/11/2007, 12h43   #4
mortee
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operator |=

Fernando Cacciola wrote:
> Hi people,
>
> Did I dream about an opeartor |=?
>
> I can't find any reference to it in the online docs, yet I think I've
> seen it in some samples.
>
> What does it do?


I can see it in the pickaxe chm, under The Ruby Language => Expressions
=> Operator Expressions.

I guess it's the same as

leftval = leftval | rightval

that is, binary OR.

mortee


  Réponse avec citation
Vieux 06/11/2007, 12h48   #5
Thomas Wieczorek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operator |=

2007/11/6, Fernando Cacciola <fernando.cacciola@gmail.com>:
> Hi people,
>
> Did I dream about an opeartor |=?
>


It exists

>
> What does it do?
>


It works for integers like the binary OR:
foo = 3 | 4 # result: 7
bar = 3
bar |= 4 # result: 7

Did you mean ||=? It is used to set default values if a variable is
not definied yet.
foo = 3
foo ||= 4 # foo doesn't change
baz ||= 4 # baz is 4 if it wasn't defined before

Regards, Thomas

  Réponse avec citation
Vieux 06/11/2007, 12h56   #6
Fernando Cacciola
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operator |=

Paul Danese wrote:
> You might be thinking about ||=


Ha, ya, that one...

Thank you.

> I believe a |= b "ORs" the bits in a and b and assigns them to a


Oh, just like in C.... who would have thought (just kiding

Best

Fernando Cacciola
SciSoft
http://fcacciola.50webs.com

  Réponse avec citation
Vieux 06/11/2007, 13h18   #7
Arlen Christian Mart Cuss
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operator |=

Hi,

On Tue, 2007-11-06 at 21:21 +0900, Eustáquio 'TaQ' Rangel wrote:
> > Did I dream about an opeartor |=?

> It sets a value if the variable does not have one, like:
>
> >> foo ||= :bar

> => :bar
> >> foo ||= :xyz

> => :bar


Please take care not to confuse ||= with |=.

||= does as you say. |= is an assignment bitwise OR.

Arlen


  Réponse avec citation
Vieux 06/11/2007, 13h20   #8
Jimmy Kofler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operator |=

> operator |=
> Posted by Fernando Cacciola (Guest) on 06.11.2007 12:59
>
> Hi people,
>
> Did I dream about an opeartor |=?
>
> I can't find any reference to it in the online docs, yet I think I've seen
> it in some samples.
>
> What does it do?


|= can also be used with arrays.

shopping_list = ['peanut butter', 'grape jelly', 'whole wheat bread']
shopping_list |= ['peanut butter', 'sugar']
p shopping_list #=> ['peanut butter', 'grape jelly', 'whole wheat
bread', 'sugar']

(cf. http://rubysnips.com/add-to-array-if )

Cheers,

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

  Réponse avec citation
Vieux 06/11/2007, 15h29   #9
Rick DeNatale
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operator |=

On 11/6/07, Paul Danese <pdanese@rib-x.com> wrote:
> You might be thinking about ||=
> ||= assigns a value to a variable if the variable is nil


To be precise, it assigns a value to a variable which is either
undefined, nil or false.
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

  Réponse avec citation
Vieux 06/11/2007, 15h31   #10
Rick DeNatale
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: operator |=

On 11/6/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:


> |= means David A. Black.



Hmmmm, I wonder if David knew that! <G>


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.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 00h26.


É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,14872 seconds with 18 queries