The ||= assignment operator

On Saturday 05 April 2008, Brian A. wrote:

lval || lval = rval

Huh?

My understanding has always been that x ||= y is shorthand for x = x

|| y just as x += y is shorthand for x = x + y. That also seems to be

the understanding of the “Programming Ruby” text (p. 125 of the
latest). In other words, an assignment will take place even if it’s
superfluous.

I also thought so, but, according to “The ruby programming language”,
it’s an
exception. It states that, if the left hand of var ||= something is not
nil or
false, no assignment is performed and, if the left hand is an array
element or
attribute, the setter method is not called.

Stefano+

On 04.04.2008 21:41, Belorion wrote:

The operators are working with a three-valued logic. In a comparison,
a FalseClass object or a NilClass object will be logistically false.

Note though that “logistical” != “logical” - two quite different pairs
of shoes. :slight_smile:

Cheers

robert

David A. Black wrote:

The only time this matters is with hashes that have default values. In
every other case, as far as I know, x = x || y also describes what’s
happening. But the expansion which describes every case is x || x =
y.

One other case (a very similar one) in which it matters is this:

class C
def x; @x || 5; end
def x=(v); puts “assigning @x=#{v}”; @x = v; end
end

c = C.new
c.x ||= 3 # no effect
p c.x # 5
c.x = 2 # assigning @x=2
p c.x # 2

Hi –

On Sun, 6 Apr 2008, Brian A. wrote:

lval || lval = rval

Huh?

My understanding has always been that x ||= y is shorthand for x = x
|| y just as x += y is shorthand for x = x + y. That also seems to be
the understanding of the “Programming Ruby” text (p. 125 of the
latest). In other words, an assignment will take place even if it’s
superfluous.

Strictly speaking, it isn’t shorthand for either, since there are
cases where the expansion will fail but ||= won’t because x isn’t
initialized. However, discounting that, the expansion is:

x || x = y

The only time this matters is with hashes that have default values. In
every other case, as far as I know, x = x || y also describes what’s
happening. But the expansion which describes every case is x || x =
y.

I wrote a blog post about this recently:
http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case
(I changed || to or for some reason, which screws up the precedence,
but I inserted corrections later.)

David

On Sat, Apr 5, 2008 at 12:08 PM, David A. Black [email protected]
wrote:

The only time this matters is with hashes that have default values. In
every other case, as far as I know, x = x || y also describes what’s
happening.

Well here’s another:

class Foo

attr_writer :x

def x
@x || 1 # !> instance variable @x not initialized
end

def inspect
“Foo(#{@x})” # !> instance variable @x not initialized
end
end

f = Foo.new
f.x # => 1
f # => Foo()
f.x ||= 2
f.x # => 1
f # => Foo()
f.x || f.x = 2
f.x # => 1
f # => Foo()
f.x = f.x || 2
f.x # => 1
f # => Foo(1)


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Sat, Apr 5, 2008 at 7:57 PM, Joel VanderWerf
[email protected] wrote:


vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Hmm I am not sure I believe you rushed your example a little bit

c.x = c.x || a
and
c.x || c.x = a
seem to me having the same effect.

I only came up with a perverse example to show the difference
class C
count = 0
def x; true end
define_method :x= do |_|
count += 1
@x = count
end
end

c = C.new
c.x || c.x = 42
p c
c.x = c.x || 42
p c
c.x ||= 42
p c

Cheers
Robert


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

On Sat, Apr 5, 2008 at 10:02 PM, David A. Black [email protected]
wrote:

seem to me having the same effect.

I don’t think so:

irb(main):017:0> c = C.new
=> #<C:0x87d0c>
irb(main):018:0> c.x || c.x = 1 # 5 || c.x = 1 => 5
=> 5
irb(main):019:0> c.instance_eval { @x }
that is true but that point was difficult to see, and I preferred an
example which shows the difference from the “normal” object interface
although my code is of course perverted.
David


Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.rubypal.com for details and updates!


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Hi –

On Sun, 6 Apr 2008, Robert D. wrote:

One other case (a very similar one) in which it matters is this:
p c.x # 2
seem to me having the same effect.
I don’t think so:

irb(main):017:0> c = C.new
=> #<C:0x87d0c>
irb(main):018:0> c.x || c.x = 1 # 5 || c.x = 1 => 5
=> 5
irb(main):019:0> c.instance_eval { @x }
=> nil
irb(main):020:0> c.x = c.x || 1 # c.x = 5 || 1 => c.x = 5
assigning @x=5
=> 5
irb(main):021:0> c.instance_eval { @x }
=> 5

David

On Sat, Apr 5, 2008 at 10:55 AM, Robert K.
[email protected] wrote:

Cheers

    robert

That’s being a little nitpicky. I think either word works in this
case, but I understand what you meant :slight_smile:

Todd

On Sat, Apr 5, 2008 at 10:39 PM, Robert D. [email protected]
wrote:

wrote:

p c.x # 5
c.x = c.x || a
=> 5
irb(main):019:0> c.instance_eval { @x }

that is true but that point was difficult to see, and I preferred an
example which shows the difference from the “normal” object interface
although my code is of course perverted.
and one cannot see the problem through the object interface either :frowning:
In an attempt to make things clear enough I will take Joel’s code again:

class << C = Class::new
def x; @x || 5 end
def get_x; @x end
def x= v; @x=v end
end

C.get_x → nil
C.x ||= 42
C.get_x → nil
C.x || C.x = 42
C.get_x → nil
C.x = C.x || 42
C.get_x → 5

Hurray I guess I finally got it.
Thx for the enlightenment.

Cheers
Robert


http://ruby-smalltalk.blogspot.com/


Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

On 05.04.2008 23:33, Todd B. wrote:

That’s being a little nitpicky.
Absolutely!

I think either word works in this
case, but I understand what you meant :slight_smile:

If by “works” you mean “can be understood” then, yes. If you say that
the word can be placed there from a language point of view, also yes. I
do believe though that “logistically” is semantically misplaced here.
:-))

Cheers

robert

On Sun, Apr 6, 2008 at 5:05 AM, Robert K.
[email protected] wrote:

On 05.04.2008 23:33, Todd B. wrote:

a FalseClass object or a NilClass object will be logistically false.

Note though that “logistical” != “logical” - two quite different pairs of shoes. :slight_smile:

That’s being a little nitpicky.
Absolutely!
I think either word works in this
case, but I understand what you meant :slight_smile:

If by “works” you mean “can be understood” then, yes. If you say that the
word can be placed there from a language point of view, also yes. I do
believe though that “logistically” is semantically misplaced here. :-))

As long as we’re being pedantic.

logical |ˈläjikəl|
adjective
of or according to the rules of logic or formal argument : a logical
impossibility.
• characterized by clear, sound reasoning : the information is
displayed in a simple and logical fashion.
• (of an action, development, decision, etc.) natural or sensible
given the circumstances : it is a logical progression from the job
before.
• capable of clear rational thinking : her logical mind.
DERIVATIVES
logically |-ik(ə)lē| |ˈlɑdʒək(ə)li| adverb : such a situation is
logically impossible.

logistics |ləˈjistiks; lō-|
plural noun [treated as sing. or pl. ]
the detailed coordination of a complex operation involving many
people, facilities, or supplies : the logistics and costs of a
vaccination campaign.
• Military the organization of moving, housing, and supplying troops
and equipment.
• the commercial activity of transporting goods to customers : [as
modifier ] Germany’s largest beverage logistics organization.

logistic |ləˈjistik; lō-|
adjective
of or relating to logistics : logistic problems.
DERIVATIVES
logistically |-tik(ə)lē| |ˈləˈdʒɪst1k(ə)li| adverb

So can’t see how “logistically false” is meaningful without a lot of
stretching.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Sun, Apr 6, 2008 at 10:13 AM, Rick DeNatale [email protected]
wrote:

On Sun, Apr 6, 2008 at 5:05 AM, Robert K.
[email protected] wrote:

On 05.04.2008 23:33, Todd B. wrote:

before.
• Military the organization of moving, housing, and supplying troops
So can’t see how “logistically false” is meaningful without a lot of stretching.
I’m not a stickler for concrete semantics in written conversation, so…

Yes, it is a stretch, but makes perfect sense to me. Maybe I didn’t
word the sentence correctly. I was referring to how you tackle the
weirdness (the logistics), not the what (the inherent logic).

Todd

On Apr 5, 12:08 pm, “David A. Black” [email protected] wrote:

superfluous.
y.

I wrote a blog post about this recently:http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case
(I changed || to or for some reason, which screws up the precedence,
but I inserted corrections later.)

Interesting - nice blog post. That is surprising, inconsistent and
unfortunate :frowning: