1

Possible Duplicate:
What does ||= mean in Ruby?

I tested like this:

>> a||=3
=> 3
>> a
=> 3
>> a||=b
=> 3
>> b
NameError: undefined local variable or method `b' for main:Object
    from (irb):11
Community
  • 1
  • 1
lkahtz
  • 4,706
  • 8
  • 46
  • 72
  • This is a duplicate of [What does ||= mean in Ruby?](http://StackOverflow.Com/q/995593/), [what is ||= in ruby?](http://StackOverflow.Com/q/3945711/), [Double Pipe Symbols in Ruby Variable Assignment?](http://StackOverflow.Com/q/4500375/), [Is the ruby operator ||= intelligent?](http://StackOverflow.Com/q/2989862/) and probably many others as well. [What does ||= mean in Ruby?](http://StackOverflow.Com/q/995593/) is even linked to in the *Related Questions* tab. Plus, there is [The definitive list of ||= (OR Equal) threads and pages](http://Ruby-Forum.Com/topic/151660/). – Jörg W Mittag Feb 26 '11 at 13:07
  • Damn, there's *another* one, even with the *exact same title* as one of the others: [What does ||= mean in Ruby?](http://StackOverflow.Com/q/3800957/). – Jörg W Mittag Feb 26 '11 at 13:30
  • 1
    Why I didn't see them on the reminder pane when I posted this... – lkahtz Feb 27 '11 at 03:49
  • @Ikahtz: Possibly because the reminder pane doesn't utilize tags, and therefore had more links to questions with "ruby" in the title. – Andrew Grimm Mar 01 '11 at 01:46

1 Answers1

5

It is the shorthand for a logical OR operation. It is equivalent to:

a || a = b

Note: The above code sample has been corrected to reflect the true (if unintuitive) behavior if expanding a ||= b. Thanks to the people who pointed that out for me. Here is the source

if a evaluates to true it will remain as is, otherwise b will be assigned to a. In ruby nil evaluates to false, so you can see how this is useful for lazy loading and default value assignment.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • thanks dude~ helps me clear the fog~~ – lkahtz Feb 26 '11 at 04:04
  • 1
    No problem. When you want to know what `*` does when used in front of an array or in a method call/signature, search for "ruby splat operator". Maybe you already know, but I remember that one taking me a while to find (searching for "*" is not easy). – Ed S. Feb 26 '11 at 04:07
  • Exactly Ed!! I searched on google and it simply ignore all such operands (even I quote/doublequote them). This is cooool~ Thanks man!! – lkahtz Feb 26 '11 at 05:29
  • 1
    This is wrong, as has been discussed a gazillion times already here on StackOverflow, on the Ruby mailing list, and on pretty much every Ruby blog ever created. – Jörg W Mittag Feb 26 '11 at 13:33
  • @ Jörg W Mittag: Thanks for that link, there's some interesting stuff there. I am not a Ruby expert, but I don't see what is "wrong" here. There are nuances that I didn't explain (or know about), but you seem to have woken up on the wrong side of the bed today. I use Ruby for simple stuff, most of the code that I write is inherently low level and not suitable for Ruby. – Ed S. Feb 26 '11 at 19:27
  • For all downvoters: Still awaiting an explanation of what is wrong here. Don't get me wrong, it's not the downvotes that I care about, I would like to know what I've missed. You don't help when you pass through, say something is wrong without explaining, and then move on. – Ed S. Feb 27 '11 at 23:55
  • 1
    @Ed S. - If you browse the duplicates, you'll find [A short-circuit (||=) edge case](http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case). Summarized: `a||=b` does not expand to `a=a||b` but `a||a=b`. It's a subtle difference but does represent different behavior as described in the article. – Corbin March Feb 28 '11 at 21:00
  • @Corbin March - Ahh, ok, that's what I was looking for (I was browsing the ruby-forum archives). Thanks. That is an odd behavior (intuitively) to be sure. – Ed S. Feb 28 '11 at 22:17
  • @Andrew: Well, the only thing I was 'complaining' about were downvotes that don't explain what is "wrong". Like I just said, I went through some of the posts on ruby-forum and didn't see what Corbin linked to, which is all that was needed. I couldn't care less about rep, but I do want to learn new things. This Jorg guy just ran through, left jerky comments, and linked to about 6 posts. I don't have time to read all of them, a simple link to a relevant topic or a simple "it expands to `a || b = a` would have been sufficient. – Ed S. Mar 01 '11 at 01:49
  • Oh, looks like Andrew deleted his comment. So sad. – Ed S. Mar 01 '11 at 01:52
  • @Ed S.: Yes, I deleted my comment. I was a bit annoyed at your comments, but then I changed my mind and decided to leave it alone. – Andrew Grimm Mar 01 '11 at 22:28
  • @Andrew Grimm: I swear I really just wanted to know what was wrong in my post for my own education, I don't care about downvotes if they are warranted :) – Ed S. Mar 01 '11 at 22:31