||=

M

Martin DeMello

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
 
L

Lyle Johnson

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

Hope this helps,

Lyle
 
R

Rob Biedenharn

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


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
(e-mail address removed)
 
T

Todd Benson

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

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
 
M

Mike Cargal

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

Mike Cargal

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

Mikael Høilund

this really is equivalent to a =3D a || b, just like a +=3D 1 is =20
equivalent to a =3D a + 1 (same for -=3D, etc.

However, be aware that this particular operator has an optimization =20
making it behave like

a || a =3D b

instead. Example problem:

default value instead of nil
=3D> {}
h[:a] =3D> "default"
h.has_key? :a
=3D> false
h[:a] ||=3D "not default" =3D> "default"
h[:a] =3D> "default"
h.has_key? :a
=3D> false
h[:a] =3D h[:a] || "not default" =3D> "default"
h[:a] =3D> "default"
h.has_key? :a =3D> true

--=20
# Mikael H=F8ilund
def method_missing(m, a=3D0) a +
m.to_s[/[a-z]+/].size * 2; end
p What is the meaning of life?
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top