Hi,
Is there an explanation somewhere of all the weird operators in ruby
such as ||= and exactly what they do?
Apologies if this is a faq but I'm unable to google anything.
a <op>= b
is completely equivalent to
a = a <op> b
So a ||= b means a = a || b. a || b evaluates to a if a is not nil or
false and to b otherwise. The side effect is that a is unchanged if not
nil or equal to b otherwise.
One great use for it is default value assignment:
@a ||= a_default_value
@a is assigned the default value if a has no value yet (nil).
HTH,
Guillaume.
···
On Sat, 2005-11-19 at 05:17 +0900, mark wrote:
Hi,
Is there an explanation somewhere of all the weird operators in ruby
such as ||= and exactly what they do?
Apologies if this is a faq but I'm unable to google anything.
mark wrote:
Hi,
Is there an explanation somewhere of all the weird operators in ruby
such as ||= and exactly what they do?
Apologies if this is a faq but I'm unable to google anything.
Operators of that form are not methods or primitives. This
x ||= 3
is equivalent to
x = x || 3
The r.h.s. has value 3 if x is undefined or nil or false.
···
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
Anything of the form:
x OP= y
is translated by Ruby to:
x = x OP y
Hope that helps.
James Edward Gray II
···
On Nov 18, 2005, at 2:17 PM, mark wrote:
Hi,
Is there an explanation somewhere of all the weird operators in ruby
such as ||= and exactly what they do?
Apologies if this is a faq but I'm unable to google anything.
got it, that's what I figured. Is there a list of these somewhere?
Thanks
mark wrote:
got it, that's what I figured. Is there a list of these somewhere?
Thanks
Here you go: http://rubygarden.org/ruby?FunnySymbolsInCode
Hi, welcome to the scene, mark.
_why
mark wrote:
thanks, why
If you still want to Google, this is usually called augmented
assignment (or reflexive assignment, or abbreviated assignment in Matz'
ruby nutshell, which is still a good thing to carry around with you for
stuff like this). In general, i don't know how you come up with Google
search terms, unless you know what they're called in perl or C or
whatever.