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
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
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
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
On Nov 6, 2007, at 5:58 AM, Fernando C. wrote:
It’s not exactly an operator itself, it is syntactic sugar.
You can find it well discussed in the archives.
|= means David A. Black.
Fernando C. 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
2007/11/6, Fernando C. [email protected]:
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
Paul D. 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 C.
SciSoft
http://fcacciola.50webs.com
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
=> :barfoo ||= :xyz
=> :bar
Please take care not to confuse ||= with |=.
||= does as you say. |= is an assignment bitwise OR.
Arlen
operator |=
Posted by Fernando C. (Guest) on 06.11.2007 12:59Hi 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.
On 11/6/07, Paul D. [email protected] 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/
On 11/6/07, John J. [email protected] wrote:
|= means David A. Black.
Hmmmm, I wonder if David knew that!
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs