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

You might be thinking about ||=

= assigns a value to a variable if the variable is nil

e.g.

x = 5
x ||= 88
x #=> 5

y = nil
y ||= 88
y # => 88

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

···

-----Original Message-----
From: news [mailto:news@ger.gmane.org] On Behalf Of Fernando Cacciola
Sent: Tuesday, November 06, 2007 6:58 AM
To: ruby-talk ML
Subject: 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

This email and any attached files are confidential and intended solely for the intended recipient(s). If you are not the named recipient you should not read, distribute, copy or alter this email. Any views or opinions expressed in this email are those of the author and do not represent those of the company. Warning: Although precautions have been taken to make sure no viruses are present in this email, the company cannot accept responsibility for any loss or damage that arise from the use of this email or attachments.

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

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.

···

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

= means David A. Black.

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

Hi people,

Did I dream about an opeartor |=?

It exists :slight_smile:

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

···

2007/11/6, Fernando Cacciola <fernando.cacciola@gmail.com>:

Paul Danese wrote:

You might be thinking about ||=

Ha, ya, that one... :wink:

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

Hi,

> 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

···

On Tue, 2007-11-06 at 21:21 +0900, Eustáquio 'TaQ' Rangel wrote:

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

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

···

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

--
Rick DeNatale

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

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

···

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

>= means David A. Black.

--
Rick DeNatale

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