operator |=

  • Thread starter Fernando Cacciola
  • Start date
F

Fernando Cacciola

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
 
E

Eustáquio 'TaQ' Rangel

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:
=> :bar

Regards,

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

"Simplicity is the ultimate sophistication."
Leonardo da Vinci
 
J

John Joyce

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

mortee

Fernando said:
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
 
T

Thomas Wieczorek

2007/11/6 said:
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
 
J

Jimmy Kofler

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

Rick DeNatale

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top