# The ||= assignment operator

**URL:** <https://rubytalk.org/t/the-assignment-operator/45672>\
**Category:** ruby-talk\
**Created:** [4 April 2008 19:11 UTC](https://rubytalk.org/t/the-assignment-operator/45672 "2008-04-04T19:11:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Belorion](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@Belorion](https://rubytalk.org/u/Belorion)\
**Post date:** [4 April 2008 19:11 UTC](https://rubytalk.org/t/the-assignment-operator/45672/1 "2008-04-04T19:11:31Z")

</div>

It was my understanding that the ||= assignment operator assigned the value  
on the right-hand side if and only if the left hand side did not already  
have a value:

irb(main):001:0\> x = true  
=\> true  
irb(main):002:0\> x ||= "ruby"  
=\> true  
irb(main):003:0\> x  
=\> true  
And, likewise, with nil:

irb(main):014:0\> x = nil  
=\> nil  
irb(main):015:0\> x ||= "ruby"  
=\> "ruby"  
irb(main):016:0\> x  
=\> "ruby"  
However, I do not understand this behavior:

irb(main):019:0\> x = false  
=\> false  
irb(main):020:0\> x ||= "ruby"  
=\> "ruby"  
irb(main):021:0\> x  
=\> "ruby"

We know that false != nil, and yet the ||= will assign if the left hand side  
is false?

regards,  
Matt

---

<div class="post-metadata">

**Author:** ![Glen\_Holcomb](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Glen\_Holcomb](https://rubytalk.org/u/Glen_Holcomb)\
**Post date:** [4 April 2008 19:25 UTC](https://rubytalk.org/t/the-assignment-operator/45672/2 "2008-04-04T19:25:04Z")

</div>

Nope ||= will assign if nil or false.

> **···**
>
> On Fri, Apr 4, 2008 at 1:11 PM, Belorion \<belorion@gmail.com\> wrote:
> 
> > It was my understanding that the ||= assignment operator assigned the  
> > value  
> > on the right-hand side if and only if the left hand side did not already  
> > have a value:
> > 
> > irb(main):001:0\> x = true  
> > =\> true  
> > irb(main):002:0\> x ||= "ruby"  
> > =\> true  
> > irb(main):003:0\> x  
> > =\> true  
> > And, likewise, with nil:
> > 
> > irb(main):014:0\> x = nil  
> > =\> nil  
> > irb(main):015:0\> x ||= "ruby"  
> > =\> "ruby"  
> > irb(main):016:0\> x  
> > =\> "ruby"  
> > However, I do not understand this behavior:
> > 
> > irb(main):019:0\> x = false  
> > =\> false  
> > irb(main):020:0\> x ||= "ruby"  
> > =\> "ruby"  
> > irb(main):021:0\> x  
> > =\> "ruby"
> > 
> > We know that false != nil, and yet the ||= will assign if the left hand  
> > side  
> > is false?
> > 
> > regards,  
> > Matt
> 
> --  
> "Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can't hear a word you're saying."
> 
> -Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [4 April 2008 19:28 UTC](https://rubytalk.org/t/the-assignment-operator/45672/3 "2008-04-04T19:28:24Z")

</div>

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

Todd

> **···**
>
> On Fri, Apr 4, 2008 at 2:11 PM, Belorion \<belorion@gmail.com\> wrote:
> 
> > It was my understanding that the ||= assignment operator assigned the value  
> > on the right-hand side if and only if the left hand side did not already  
> > have a value:
> > 
> > irb(main):001:0\> x = true  
> > =\> true  
> > irb(main):002:0\> x ||= "ruby"  
> > =\> true  
> > irb(main):003:0\> x  
> > =\> true  
> > And, likewise, with nil:
> > 
> > irb(main):014:0\> x = nil  
> > =\> nil  
> > irb(main):015:0\> x ||= "ruby"  
> > =\> "ruby"  
> > irb(main):016:0\> x  
> > =\> "ruby"  
> > However, I do not understand this behavior:
> > 
> > irb(main):019:0\> x = false  
> > =\> false  
> > irb(main):020:0\> x ||= "ruby"  
> > =\> "ruby"  
> > irb(main):021:0\> x  
> > =\> "ruby"
> > 
> > We know that false != nil, and yet the ||= will assign if the left hand side  
> > is false?
> > 
> > regards,  
> > Matt

---

<div class="post-metadata">

**Author:** ![Paul\_Mucur](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@Paul\_Mucur](https://rubytalk.org/u/Paul_Mucur)\
**Post date:** [4 April 2008 19:30 UTC](https://rubytalk.org/t/the-assignment-operator/45672/4 "2008-04-04T19:30:29Z")

</div>

It's probably better to think of x ||= "ruby" as being short hand for x = x || "ruby" (which it effectively is) instead of "assign if not already having a value". If you do this, then you can see why your last example behaves like it does.

Don't forget that there are other similar operators like +=, -= and &&= and they all follow the same pattern:

x ?= y  
x = x ? y

Where ? is one of -, +, && or ||.

Hope that this helps.

> **···**
>
> On 4 Apr 2008, at 20:11, Belorion wrote:
> 
> > It was my understanding that the ||= assignment operator assigned the value  
> > on the right-hand side if and only if the left hand side did not already  
> > have a value:

---

<div class="post-metadata">

**Author:** ![Belorion](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@Belorion](https://rubytalk.org/u/Belorion)\
**Post date:** [4 April 2008 19:41 UTC](https://rubytalk.org/t/the-assignment-operator/45672/5 "2008-04-04T19:41:38Z")

</div>

Thanks all, that clears up my understanding. I hadn't thought of it as x =  
x || "ruby" (even thoughI knew that x += 1 is the same as x = x + 1) and  
that in that expanded case, x evaluates, logistically, to false if nil or  
false.

Matt

> **···**
>
> On Fri, Apr 4, 2008 at 2:28 PM, Todd Benson \<caduceass@gmail.com\> wrote:
> 
> > On Fri, Apr 4, 2008 at 2:11 PM, Belorion \<belorion@gmail.com\> wrote:  
> > \> It was my understanding that the ||= assignment operator assigned the  
> > value  
> > \> on the right-hand side if and only if the left hand side did not  
> > already  
> > \> have a value:  
> > \>  
> > \> irb(main):001:0\> x = true  
> > \> =\> true  
> > \> irb(main):002:0\> x ||= "ruby"  
> > \> =\> true  
> > \> irb(main):003:0\> x  
> > \> =\> true  
> > \> And, likewise, with nil:  
> > \>  
> > \> irb(main):014:0\> x = nil  
> > \> =\> nil  
> > \> irb(main):015:0\> x ||= "ruby"  
> > \> =\> "ruby"  
> > \> irb(main):016:0\> x  
> > \> =\> "ruby"  
> > \> However, I do not understand this behavior:  
> > \>  
> > \> irb(main):019:0\> x = false  
> > \> =\> false  
> > \> irb(main):020:0\> x ||= "ruby"  
> > \> =\> "ruby"  
> > \> irb(main):021:0\> x  
> > \> =\> "ruby"  
> > \>  
> > \>  
> > \> We know that false != nil, and yet the ||= will assign if the left hand  
> > side  
> > \> is false?  
> > \>  
> > \> regards,  
> > \> Matt
> > 
> > The operators are working with a three-valued logic. In a comparison,  
> > a FalseClass object or a NilClass object will be logistically false.
> > 
> > Todd

---

<div class="post-metadata">

**Author:** ![Chris\_Shea1](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@Chris\_Shea1](https://rubytalk.org/u/Chris_Shea1)\
**Post date:** [4 April 2008 20:15 UTC](https://rubytalk.org/t/the-assignment-operator/45672/6 "2008-04-04T20:15:08Z")

</div>

It's better to think of x ||= "ruby" as x || x = "ruby".

> **···**
>
> On Apr 4, 1:30 pm, Paul Mucur \<mu...@waferbaby.com\> wrote:
> 
> > On 4 Apr 2008, at 20:11, Belorion wrote:
> > 
> > \> It was my understanding that the ||= assignment operator assigned  
> > \> the value  
> > \> on the right-hand side if and only if the left hand side did not  
> > \> already  
> > \> have a value:
> > 
> > It's probably better to think of x ||= "ruby" as being short hand for  
> > x = x || "ruby" (which it effectively is) instead of "assign if not  
> > already having a value". If you do this, then you can see why your  
> > last example behaves like it does.
> > 
> > Don't forget that there are other similar operators like +=, -= and  
> > &&= and they all follow the same pattern:
> > 
> > x ?= y  
> > x = x ? y
> > 
> > Where ? is one of -, +, && or ||.
> > 
> > Hope that this helps.
> 
> --  
> h = Hash.new('default value')
> 
> h[:test] ||= 'assigned value'  
> h # =\> {}
> 
> # same as:  
> h[:test] || h[:test] = 'assigned value'  
> h # =\> {}
> 
> # not:  
> h[:test] = h[:test] || 'assigned value'  
> h # =\> {:test=\>"default value"}  
> --
> 
> Chris

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [4 April 2008 19:51 UTC](https://rubytalk.org/t/the-assignment-operator/45672/7 "2008-04-04T19:51:54Z")

</div>

For conditional expressions. Here's something that may catch you off guard...

irb(main):001:0\> nil || false  
=\> false  
irb(main):001:0\> false || nil  
=\> nil

Todd

> **···**
>
> On Fri, Apr 4, 2008 at 2:41 PM, Belorion \<belorion@gmail.com\> wrote:
> 
> > Thanks all, that clears up my understanding. I hadn't thought of it as x =  
> > x || "ruby" (even thoughI knew that x += 1 is the same as x = x + 1) and  
> > that in that expanded case, x evaluates, logistically, to false if nil or  
> > false.

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [5 April 2008 03:29 UTC](https://rubytalk.org/t/the-assignment-operator/45672/8 "2008-04-05T03:29:04Z")

</div>

# It's better to think of x ||= "ruby" as x || x = "ruby".

> **···**
>
> From: Chris Shea [[mailto:cmshea@gmail.com](mailto:cmshea@gmail.com)]  
> #  
> # --  
> # h = Hash.new('default value')  
> #  
> # h[:test] ||= 'assigned value'  
> # h # =\> {}  
> #  
> # # same as:  
> # h[:test] || h[:test] = 'assigned value'  
> # h # =\> {}  
> #  
> # # not:  
> # h[:test] = h[:test] || 'assigned value'  
> # h # =\> {:test=\>"default value"}
> 
> i'd say that's a bug in design (unless x+=1 is now x+x=1 😉
> 
> kind regards -botp

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [5 April 2008 15:55 UTC](https://rubytalk.org/t/the-assignment-operator/45672/9 "2008-04-05T15:55:06Z")

</div>

Note though that "logistical" != "logical" - two quite different pairs of shoes. 🙂

Cheers

&nbsp;&nbsp;robert

> **···**
>
> 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.

---

<div class="post-metadata">

**Author:** ![Avdi\_Grimm1](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Avdi\_Grimm1](https://rubytalk.org/u/Avdi_Grimm1)\
**Post date:** [5 April 2008 03:53 UTC](https://rubytalk.org/t/the-assignment-operator/45672/10 "2008-04-05T03:53:17Z")

</div>

On this topic, am I the only one who would really like to see a  
dedicated "defaulting operator" in Ruby? Something that actually has  
the semantics which a lot of people mistakenly assume for ||=, i.e.  
"assign if and only if nil"? I think Perl6 uses the //= operator for  
this. This is such a subtle gotcha. I've seen a lot of code that was  
potentially buggy because it didn't account for the fact that the  
'false' value would be handled incorrectly.

> **···**
>
> --  
> Avdi

---

<div class="post-metadata">

**Author:** ![Joshua\_Ballanco](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@Joshua\_Ballanco](https://rubytalk.org/u/Joshua_Ballanco)\
**Post date:** [5 April 2008 05:46 UTC](https://rubytalk.org/t/the-assignment-operator/45672/11 "2008-04-05T05:46:11Z")

</div>

Peña, Botp wrote:

> From: Chris Shea [mailto:cmshea@gmail.com]  
> # It's better to think of x ||= "ruby" as x || x = "ruby".  
> #  
> # --  
> # h = Hash.new('default value')  
> #  
> # h[:test] ||= 'assigned value'  
> # h # =\> {}  
> #  
> # # same as:  
> # h[:test] || h[:test] = 'assigned value'  
> # h # =\> {}  
> #  
> # # not:  
> # h[:test] = h[:test] || 'assigned value'  
> # h # =\> {:test=\>"default value"}
> 
> i'd say that's a bug in design (unless x+=1 is now x+x=1 😉
> 
> kind regards -botp

Not at all. Rather, this is just a subtle misunderstanding of how hash  
is implemented. Consider the following:

> > h = Hash.new

=\> {}

> > h[:test] ||= 'testing without default'

=\> "testing without default"

> > h

=\> {:test=\>"testing without default"}

> > h = Hash.new('default value')

=\> {}

> > h[:test] ||= 'testing with default'

=\> "default value"

> > h

=\> {}

The only reason that Chris' example behaves like "x || x = stuff" is  
because he's defined a default value for the hash. If you set a default  
value, than you'll never have a keyed value be empty (i.e. nil).  
Consider further:

> > h = Hash.new

=\> {}

> > puts 'empty' unless h[:test]

empty  
=\> nil

> > h = Hash.new('default value')

=\> {}

> > puts 'empty' unless h[:test]

=\> nil

> >

I think the confusion is that, in Chris' example, there's no assignment,  
so the hash only holds the default value temporarily (i.e. just long  
enough to not evaluate to nil or false).

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [5 April 2008 21:33 UTC](https://rubytalk.org/t/the-assignment-operator/45672/12 "2008-04-05T21:33:06Z")

</div>

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

Todd

> **···**
>
> On Sat, Apr 5, 2008 at 10:55 AM, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > 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. 🙂
> > 
> > Cheers
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Steve\_Ross](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Steve\_Ross](https://rubytalk.org/u/Steve_Ross)\
**Post date:** [5 April 2008 04:38 UTC](https://rubytalk.org/t/the-assignment-operator/45672/13 "2008-04-05T04:38:42Z")

</div>

This would be great, but you would need both a // and //= operator. Consider this:

@my\_web\_page\_title = @page\_name || "d'oh! you forgot to name your page"

You would want to rewrite this as:

@my\_web\_page\_title = @page\_name // "d'oh! you forgot to name your page"

It would seem that adding these operators has a low potential to introduce bugs into working code and, as you point out, there are subtleties WRT the ||= idiom. Moving it from idiom to first-class operator might clarify those subtleties.

> **···**
>
> On Apr 4, 2008, at 8:53 PM, Avdi Grimm wrote:
> 
> > On this topic, am I the only one who would really like to see a  
> > dedicated "defaulting operator" in Ruby? Something that actually has  
> > the semantics which a lot of people mistakenly assume for ||=, i.e.  
> > "assign if and only if nil"? I think Perl6 uses the //= operator for  
> > this. This is such a subtle gotcha. I've seen a lot of code that was  
> > potentially buggy because it didn't account for the fact that the  
> > 'false' value would be handled incorrectly.
> > 
> > --  
> > Avdi

---

<div class="post-metadata">

**Author:** ![Yossef\_Mendelssohn](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yossef\_Mendelssohn](https://rubytalk.org/u/Yossef_Mendelssohn)\
**Post date:** [5 April 2008 07:07 UTC](https://rubytalk.org/t/the-assignment-operator/45672/14 "2008-04-05T07:07:02Z")

</div>

> Peña, Botp wrote:  
> \> From: Chris Shea [mailto:cms...@gmail.com]  
> \> # It's better to think of x ||= "ruby" as x || x = "ruby".  
> \> #  
> \> # --  
> \> # h = Hash.new('default value')  
> \> #  
> \> # h[:test] ||= 'assigned value'  
> \> # h # =\> {}  
> \> #  
> \> # # same as:  
> \> # h[:test] || h[:test] = 'assigned value'  
> \> # h # =\> {}  
> \> #  
> \> # # not:  
> \> # h[:test] = h[:test] || 'assigned value'  
> \> # h # =\> {:test=\>"default value"}
> 
> \> i'd say that's a bug in design (unless x+=1 is now x+x=1 😉
> 
> \> kind regards -botp
> 
> Not at all. Rather, this is just a subtle misunderstanding of how hash  
> is implemented.

\<snip\>

> The only reason that Chris' example behaves like "x || x = stuff" is  
> because he's defined a default value for the hash. If you set a default  
> value, than you'll never have a keyed value be empty (i.e. nil).  
> Consider further:

\<snip\>

> I think the confusion is that, in Chris' example, there's no assignment,  
> so the hash only holds the default value temporarily (i.e. just long  
> enough to not evaluate to nil or false).  
> --  
> Posted viahttp://www.ruby-forum.com/.

There was a long thread about this some time ago, and I'm too tired to  
search for it now. In the end, it came down a difference of opinion.

Some people say this violates their expectations because they're told  
that `var op= value` translates to `var = var op value` always.

Other people say this is expected because `var ||= value` really means  
`var = value unless var` and everybody should know that.

Personally, I am in the former camp. I think of the latter as an  
optimization that works in many places, but optimizations are usually  
a form of cheating and cheaters eventually get caught. In Ruby, this  
cheater gets caught in the web of deceit centered around hashes with  
default values.

Note that `hash_with_default_value[key] ||= val` is the only place  
where this is even an issue.

> **···**
>
> On Apr 5, 12:46 am, Joshua Ballanco \<jball...@gmail.com\> wrote:
> 
> --  
> -yossef

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [5 April 2008 08:25 UTC](https://rubytalk.org/t/the-assignment-operator/45672/15 "2008-04-05T08:25:46Z")

</div>

# Not at all. Rather, this is just a subtle misunderstanding of  
# how hash is implemented. Consider the following:  
# \>\> h = Hash.new  
# =\> {}  
# \>\> h[:test] ||= 'testing without default'  
# =\> "testing without default"  
# \>\> h  
# =\> {:test=\>"testing without default"}  
# \>\> h = Hash.new('default value')  
# =\> {}  
# \>\> h[:test] ||= 'testing with default'  
# =\> "default value"  
# \>\> h  
# =\> {}  
# The only reason that Chris' example behaves like "x || x = stuff" is  
# because he's defined a default value for the hash. If you set  
# a default value, than you'll never have a keyed value be empty  
# (i.e. nil). Consider further:  
# \>\> h = Hash.new  
# =\> {}  
# \>\> puts 'empty' unless h[:test]  
# empty  
# =\> nil  
# \>\> h = Hash.new('default value')  
# =\> {}  
# \>\> puts 'empty' unless h[:test]  
# =\> nil

my question is simple,

given,

h = Hash.new('default value')  
#=\> {}

why is

h[:test] ||= 'this is alternative'  
#=\> "default value"  
h  
#=\> {}

different from

h[:test] = h[:test] || 'this is alternative'  
#=\> "default value"  
h  
#=\> {:test=\>"default value"}

??

That is not a nice surprise, imho; and the (syntax) sugar is not sweet 🙂

kind regards -botp

> **···**
>
> From: Joshua Ballanco [[mailto:jballanc@gmail.com](mailto:jballanc@gmail.com)]

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [5 April 2008 10:13 UTC](https://rubytalk.org/t/the-assignment-operator/45672/16 "2008-04-05T10:13:27Z")

</div>

Hi --

> On this topic, am I the only one who would really like to see a  
> dedicated "defaulting operator" in Ruby? Something that actually has  
> the semantics which a lot of people mistakenly assume for ||=, i.e.  
> "assign if and only if nil"? I think Perl6 uses the //= operator for  
> this. This is such a subtle gotcha. I've seen a lot of code that was  
> potentially buggy because it didn't account for the fact that the  
> 'false' value would be handled incorrectly.

The false value isn't handled incorrectly, though. The test is for  
boolean falseness, and both nil and false always pass that test. If  
you need x to be a certain value (nil or any other), you should test  
for exactly that value.

Also, it's not exactly that the lhs of ||= is nil. ||= will allow  
uninitialized local variables, which are not nil:

irb(main):003:0\> a ||= 1  
=\> 1  
irb(main):004:0\> b || b = 1  
NameError: undefined local variable or method `b' for main:Object  
&nbsp;&nbsp;&nbsp;from (irb):4

So we'd be getting into a thing with "uninitialized or nil, but not  
false" which seems very use-case specific to me. I'd rather let the  
boolean significance just do its thing.

David

> **···**
>
> On Sat, 5 Apr 2008, Avdi Grimm wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS April 14-17 New York City  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [5 April 2008 10:20 UTC](https://rubytalk.org/t/the-assignment-operator/45672/17 "2008-04-05T10:20:26Z")

</div>

HI --

> Peña, Botp wrote:
> 
> > From: Chris Shea [mailto:cmshea@gmail.com]  
> > # It's better to think of x ||= "ruby" as x || x = "ruby".  
> > #  
> > # --  
> > # h = Hash.new('default value')  
> > #  
> > # h[:test] ||= 'assigned value'  
> > # h # =\> {}  
> > #  
> > # # same as:  
> > # h[:test] || h[:test] = 'assigned value'  
> > # h # =\> {}  
> > #  
> > # # not:  
> > # h[:test] = h[:test] || 'assigned value'  
> > # h # =\> {:test=\>"default value"}
> > 
> > i'd say that's a bug in design (unless x+=1 is now x+x=1 😉
> > 
> > kind regards -botp
> 
> Not at all. Rather, this is just a subtle misunderstanding of how hash  
> is implemented. Consider the following:
> 
> > > h = Hash.new
> 
> =\> {}
> 
> > > h[:test] ||= 'testing without default'
> 
> =\> "testing without default"
> 
> > > h
> 
> =\> {:test=\>"testing without default"}
> 
> > > h = Hash.new('default value')
> 
> =\> {}
> 
> > > h[:test] ||= 'testing with default'
> 
> =\> "default value"
> 
> > > h
> 
> =\> {}
> 
> The only reason that Chris' example behaves like "x || x = stuff" is  
> because he's defined a default value for the hash. If you set a default  
> value, than you'll never have a keyed value be empty (i.e. nil).

The only expansion that accounts for all cases of the ||= operator is  
x || x = stuff. If it were actually x = x || stuff, then this:

irb(main):005:0\> h = Hash.new(1)  
=\> {}  
irb(main):006:0\> h[:x] = h[:x] || 2  
=\> 1  
irb(main):007:0\> h  
=\> {:x=\>1}

would behave the same as this:

irb(main):008:0\> h[:y] ||= 2  
=\> 1  
irb(main):009:0\> h  
=\> {:x=\>1}

but it doesn't. But ||= does behave like this:

irb(main):010:0\> h[:z] || h[:z] = 2  
=\> 1  
irb(main):011:0\> h  
=\> {:x=\>1}

In other words, x ||= y == x || x = y, or something 🙂

I personally would like to see it act like x = x || y, though I think  
hash defaults are the only case where the difference actually makes a  
difference.

David

> **···**
>
> On Sat, 5 Apr 2008, Joshua Ballanco wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS April 14-17 New York City  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [6 April 2008 09:05 UTC](https://rubytalk.org/t/the-assignment-operator/45672/18 "2008-04-06T09:05:06Z")

</div>

> > > > 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. 🙂
> 
> That's being a little nitpicky.

Absolutely!

> I think either word works in this  
> case, but I understand what you meant 🙂

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

&nbsp;&nbsp;robert

> **···**
>
> On 05.04.2008 23:33, Todd Benson wrote:
> 
> > On Sat, Apr 5, 2008 at 10:55 AM, Robert Klemme \> \<shortcutter@googlemail.com\> wrote:
> > 
> > > On 04.04.2008 21:41, Belorion wrote:

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [5 April 2008 12:24 UTC](https://rubytalk.org/t/the-assignment-operator/45672/19 "2008-04-05T12:24:43Z")

</div>

I respectfully disagree, this is very well defined behavior, I  
partially blame the list which \*very\* often explained to newbies that

@x ||= value

assignes value to @x when it was not defined before. It is sad that  
things are incorrectly explained, that is all.  
Furthermore things are simple

(1) lval ||= rval is lval = lval || rval  
(2) nil || x = x  
(3) false || x = x  
(4) non initialized ivars evaluate to nil

once one accepts these basic rules of the language there is no surprise.  
I like the syntactic sugar but to use a recently used quote "DE  
GVSTIBVS NON DISPVTANDVM EST"

Cheers  
Robert

> **···**
>
> On Sat, Apr 5, 2008 at 10:25 AM, Peña, Botp \<botp@delmonte-phil.com\> wrote:
> 
> > From: Joshua Ballanco [mailto:jballanc@gmail.com]  
> > # Not at all. Rather, this is just a subtle misunderstanding of
> > 
> > # how hash is implemented. Consider the following:  
> > # \>\> h = Hash.new  
> > # =\> {}  
> > # \>\> h[:test] ||= 'testing without default'  
> > # =\> "testing without default"  
> > # \>\> h  
> > # =\> {:test=\>"testing without default"}  
> > # \>\> h = Hash.new('default value')  
> > # =\> {}  
> > # \>\> h[:test] ||= 'testing with default'  
> > # =\> "default value"  
> > # \>\> h  
> > # =\> {}  
> > # The only reason that Chris' example behaves like "x || x = stuff" is  
> > # because he's defined a default value for the hash. If you set  
> > # a default value, than you'll never have a keyed value be empty  
> > # (i.e. nil). Consider further:  
> > # \>\> h = Hash.new  
> > # =\> {}  
> > # \>\> puts 'empty' unless h[:test]  
> > # empty  
> > # =\> nil  
> > # \>\> h = Hash.new('default value')  
> > # =\> {}  
> > # \>\> puts 'empty' unless h[:test]  
> > # =\> nil
> > 
> > my question is simple,
> > 
> > given,
> > 
> > h = Hash.new('default value')  
> > #=\> {}
> > 
> > why is
> > 
> > h[:test] ||= 'this is alternative'  
> > #=\> "default value"  
> > h  
> > #=\> {}
> > 
> > different from
> > 
> > h[:test] = h[:test] || 'this is alternative'  
> > #=\> "default value"  
> > h  
> > #=\> {:test=\>"default value"}
> > 
> > ??
> > 
> > That is not a nice surprise, imho; and the (syntax) sugar is not sweet 🙂
> > 
> > kind regards -botp
> 
> --  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> ---  
> Whereof one cannot speak, thereof one must be silent.  
> Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![Avdi\_Grimm1](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Avdi\_Grimm1](https://rubytalk.org/u/Avdi_Grimm1)\
**Post date:** [5 April 2008 14:20 UTC](https://rubytalk.org/t/the-assignment-operator/45672/20 "2008-04-05T14:20:13Z")

</div>

On Sat, Apr 5, 2008 at 6:13 AM, David A. Black

> The false value isn't handled incorrectly, though.

When I wrote "handled incorrectly", I didn't mean that Ruby handles it  
incorrectly; I meant that I've seen a great deal of code people had  
written which was incorrect in the face of explicit 'false' values  
because of this very common misunderstanding of ||=.

> The test is for  
> boolean falseness, and both nil and false always pass that test. If  
> you need x to be a certain value (nil or any other), you should test  
> for exactly that value.

I agree. Which is why I often use something like  
&nbsp;&nbsp;if x.nil? then x = y end  
which isn't nearly as concise and expressive as x ||= y. It could be  
somewhat shortened to:  
&nbsp;&nbsp;x.nil? && x = y  
but I don't find that particularly readable. In any case, \*neither\*  
of them conveniently handles the case where x is undefined as well as  
the case where x is nil, the way ||= does, as you note below:

> Also, it's not exactly that the lhs of ||= is nil. ||= will allow  
> uninitialized local variables, which are not nil:

[...]

> So we'd be getting into a thing with "uninitialized or nil, but not  
> false" which seems very use-case specific to me.

The core of my argument is that this idiom isn't rare or use-case  
specific at all. is, in my experience of both writing and reading Ruby  
code, an \*extremely\* common pattern. Being able to concisely and  
unambiguously say "after this line, x (or h[:x]) is guaranteed to be  
initialized and non-nil, either by previous assignment or by the  
specified default" is something I see a need for every working day.  
And because it's such a common need, and because ||= seems so  
deceptively close to fulfilling it, I have seen many, many examples of  
code that was subtly incorrect because it used ||=. Ruby is a  
language that often offers sugar for common idioms, and this is a very  
common idiom.

> **···**
>
> --  
> Avdi
> 
> --  
> Avdi

[Next page](https://rubytalk.org/t/the-assignment-operator/45672.md?page=2)
