what does ||= do?
···
--
Posted via http://www.ruby-forum.com/.
what does ||= do?
--
Posted via http://www.ruby-forum.com/.
irb is your friend
Todd
On Wed, Jun 11, 2008 at 3:59 PM, Justin To <tekmc@hotmail.com> wrote:
what does ||= do?
what does ||= do?
a ||= b is shorthand for a = a || b
is the "or" operator - a || b evaluates to a unless a is nil or
false, in which case it evaluates to b
a ||= b is most commonly used to say "if a hasn't already been set
(and is therefore nil), set it to b, else leave it alone"
martin
On Wed, Jun 11, 2008 at 1:59 PM, Justin To <tekmc@hotmail.com> wrote:
The expression:
x ||= y
is shorthand for:
x = x || y
where "||" is the OR-operator. If x evaluates as false or nil, the
value of y will be assigned to x; otherwise, x keeps its original
value. So, for example,
x = nil
x ||= 42 # now x has value 42
x ||= 23 # x stays at 42
Hope this helps,
Lyle
On Wed, Jun 11, 2008 at 3:59 PM, Justin To <tekmc@hotmail.com> wrote:
what does ||= do?
Justin To wrote:
what does ||= do?
In other languages you would code this something like:
if a == nil
a = b
end
by
TheR
--
Posted via http://www.ruby-forum.com/\.
Just to short-circuit the discussion:
a ||= b
is actually implemented as if:
a || a=b
Search for recent threads or find David A. Black's blog.
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
On Jun 11, 2008, at 5:12 PM, Martin DeMello wrote:
On Wed, Jun 11, 2008 at 1:59 PM, Justin To <tekmc@hotmail.com> wrote:
what does ||= do?
a ||= b is shorthand for a = a || b
>> is the "or" operator - a || b evaluates to a unless a is nil or
false, in which case it evaluates to ba ||= b is most commonly used to say "if a hasn't already been set
(and is therefore nil), set it to b, else leave it alone"martin
Actually, it is like:
x || x=y
Refer to:
http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case
-Rob
On Jun 11, 2008, at 5:12 PM, Lyle Johnson wrote:
On Wed, Jun 11, 2008 at 3:59 PM, Justin To <tekmc@hotmail.com> wrote:
what does ||= do?
The expression:
x ||= y
is shorthand for:
x = x || y
where "||" is the OR-operator. If x evaluates as false or nil, the
value of y will be assigned to x; otherwise, x keeps its original
value. So, for example,x = nil
x ||= 42 # now x has value 42
x ||= 23 # x stays at 42Hope this helps,
Lyle
Not exactly.
a = 2
a ||= b #where b is not defined
p a #we get 2
p b #we get NameError
Not only that, but there are other subtleties that many gurus on this
list have pointed out.
Todd
On Wed, Jun 11, 2008 at 4:12 PM, Lyle Johnson <lyle@lylejohnson.name> wrote:
On Wed, Jun 11, 2008 at 3:59 PM, Justin To <tekmc@hotmail.com> wrote:
what does ||= do?
The expression:
x ||= y
is shorthand for:
x = x || y
where "||" is the OR-operator. If x evaluates as false or nil, the
value of y will be assigned to x; otherwise, x keeps its original
value. So, for example,x = nil
x ||= 42 # now x has value 42
x ||= 23 # x stays at 42
careful..
this is really...
if not a
a = b
end
(it will assign b if a resolves to false as a boolean (i.e. either nil or false)
On Jun 12, 2008, at 1:57 AM, Damjan Rems wrote:
Justin To wrote:
what does ||= do?
In other languages you would code this something like:
if a == nil
a = b
endby
TheR
--
Posted via http://www.ruby-forum.com/\.
(need to finish replies before clicking "send")
this really is equivalent to a = a || b, just like a += 1 is equivalent to a = a + 1 (same for -=, etc.)
that's why you get b if a was false
On Jun 12, 2008, at 1:57 AM, Damjan Rems wrote:
Justin To wrote:
what does ||= do?
In other languages you would code this something like:
if a == nil
a = b
endby
TheR
--
Posted via http://www.ruby-forum.com/\.
this really is equivalent to a = a || b, just like a += 1 is equivalent to a = a + 1 (same for -=, etc.
However, be aware that this particular operator has an optimization making it behave like
a || a = b
instead. Example problem:
>> h = Hash.new() { "default" } # Makes a Hash with "default" as the default value instead of nil
=> {}
>> h[:a]
=> "default"
>> h.has_key? :a
=> false
>> h[:a] ||= "not default"
=> "default"
>> h[:a]
=> "default"
>> h.has_key? :a
=> false
>> h[:a] = h[:a] || "not default"
=> "default"
>> h[:a]
=> "default"
>> h.has_key? :a
=> true
On Jun 13, 2008, at 0:08, Mike Cargal wrote:
>>
--
# Mikael Høilund
def method_missing(m, a=0) a +
m.to_s[/[a-z]+/].size * 2; end
p What is the meaning of life?