Logical OR in Ruby

P

Paul Sholtz

I'm looking at some source code, to try and understand it.

There's a variable "weight", and then there's this call:

weight ||= 100

which I take to be equivalent to:

weight = weight || 100

(i.e., performing a logical OR between "weight" and 100).

What I think this code means, is that the value that will be assigned to
"weight" will be either (a) "weight" (if weight has already been
assigned) or else (b) if "weight" is presently nil, then the value 100
will be assigned to "weight" instead.

Is this correct?
 
P

pat eyler

I'm looking at some source code, to try and understand it.

There's a variable "weight", and then there's this call:

weight ||=3D 100

which I take to be equivalent to:

weight =3D weight || 100

(i.e., performing a logical OR between "weight" and 100).

What I think this code means, is that the value that will be assigned to
"weight" will be either (a) "weight" (if weight has already been
assigned) or else (b) if "weight" is presently nil, then the value 100
will be assigned to "weight" instead.

Is this correct?

yes.

Generally, you'll find irb to be very helpful in finding things like this o=
ut:

irb(main):001:0> foo ||=3D 10
=3D> 10
irb(main):002:0> foo
=3D> 10
irb(main):003:0> foo ||=3D100
=3D> 10
irb(main):004:0> foo
=3D> 10
irb(main):005:0>




--=20
thanks,
-pate
 
M

Michael Edgar

More specifically, || returns the second operand when the first operand =
is "falsy" -
logically false. Both nil and false are logically false.

So if "weight" were either nil OR false, then it would be reassigned.

Michael Edgar
(e-mail address removed)
http://carboni.ca/
 
S

Stefano Mioli

I'm looking at some source code, to try and understand it.
[snip]

What I think this code means, is that the value that will be assigned to
"weight" will be either (a) "weight" (if weight has already been
assigned) or else (b) if "weight" is presently nil, then the value 100
will be assigned to "weight" instead.

Is this correct?

Yep, as others told you.

It might be worth pointing out, though, that Ruby might have a different
idea of falsehood compared to other languages you might be used to:

irb(main):001:0> puts "hey" if 0
hey
=> nil
irb(main):002:0> puts "hey" if []
hey
=> nil

In Python 0 and [] would both evaluate to False.
 
C

Colin Bartlett

I'm looking at some source code, to try and understand it.
There's a variable "weight", and then there's this call:
weight ||=3D 100
which I take to be equivalent to:
weight =3D weight || 100
(i.e., performing a logical OR between "weight" and 100).

Not quite correct. there's a useful explanation here:
http://talklikeaduck.denhaven2.com/2008/04/26/x-y-redux
which (providing I'm reading Rick DeNatale's blog correctly) tells us that
(weight ||=3D 100) is expanded in Ruby as (weight || (weight =3D 100))
which *mostly* is the same as (weight =3D (weight || 100)), but might be
different (to quote Rick DeNatale) "when the left hand side is a
method call, to an accessor, or accessor-like method".

*** an extract from Rick DeNatale's blog post:

Matz explains that the real expansion of x ||=3D y is: x || x =3D y

The expectation that x ||=3D y is the same as x =3D x || y, does seem
reasonable to someone =91coming from=92 C or one of it=92s derivative
languages. As far as I can determine, C introduced the notion of
assignment operators like +=3D and -=3D. And K&R defined these assignment
operators as a shorthand for x =3D x + y, etc.

On the other hand, although C has logical operators || and && which,
like Ruby have =91short-circuit=92 evaluation, it doesn=92t allow ||=3D, or
&&=3D as assignment operators.

Since || is a =91short-circuit=92 boolean operator, the right hand operand
expression is only evaluated if the left hand operand expression
evaluates to a logically false value, i.e. either nil or false.

The way that Matz included ||=3D as an assignment operator makes perfect
sense to me. The ||=3D assignment operator reserves the short-circuit
nature of ||.

*** Rick DeNatale also points out that &&=3D has similar behaviour.
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top