[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

operator |=

Fernando Cacciola

11/6/2007 11:58:00 AM

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



7 Answers

Eustaquio 'TaQ' Rangel

11/6/2007 12:22:00 PM

0

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

"Simplicity is the ultimate sophistication."
Leonardo da Vinci

John Joyce

11/6/2007 12:32:00 PM

0


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.

mortee

11/6/2007 12:44:00 PM

0

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


Thomas Wieczorek

11/6/2007 12:49:00 PM

0

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

Arlen Cuss

11/6/2007 1:19:00 PM

0

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


Jimmy Kofler

11/6/2007 1:20:00 PM

0

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

Cheers,

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

Rick DeNatale

11/6/2007 3:32:00 PM

0

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