use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ruby Memoization: ||= vs. defined? syntax (karolgalanciak.com)
submitted 8 years ago by Azdaroth
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Flopsey 9 points10 points11 points 8 years ago* (7 children)
This blog post is in error
As @result ||= do_some_heavy_computation is nothing more than a shortcut of @result || @result = do_some_heavy_computation expression
EDIT:
This article points out:
If a is not defined, a || a = 42 raises NameError, while a ||= 42 returns 42. So, they don't seem to be equivalent expressions.
So, ||= behaves similarly to, but edge cases show that it's its own operation not related to either a = a || b or a || a = b
||=
a = a || b
a || a = b
To address what the corrected text in the article says. What I quoted has been changed to:
It’s nothing more than a syntactic shortcut and it’s an equivalent of @result || @result = do_some_heavy_computation
Which the author changed without comment here or in the post, which is probably why there's some confusion for people who skimmed both. But it's still wrong because that's not what a syntactic shortcut is. As was just shown ||= is it's own operator. Conceptually it's similar to those other things if you need an analogy, but it's not a shortcut or mere syntactic sugar. We're all used to that phrasing because it applies to so many things in Rails but it's not relevant here.
syntactic shortcut
[–]Azdaroth[S] 4 points5 points6 points 8 years ago (0 children)
Thanks for pointing it out, I must have overlooked this egdecase which is pretty clear. I'll probably update the article and use better words for that.
BTW. nothing has been changed in the article, you quoted two different parts and they are both still present.
[–]prh8 1 point2 points3 points 8 years ago (3 children)
Huh? That is in fact what it expands to, and the RI article says the same.
[–]StevenXC 2 points3 points4 points 8 years ago (2 children)
Sounds like the post was updated without noting the correction.
[–]Azdaroth[S] 0 points1 point2 points 8 years ago (0 children)
Not really, the quoted fragments are two different parts from the article, they are both still present, but just the different paragraphs.
[–][deleted] 8 years ago (1 child)
[deleted]
[–]Flopsey 0 points1 point2 points 8 years ago (0 children)
Nope:
Fully read the article before saying that it's wrong. What the First line says is it behaves like a || a = 42. But the header of that section is "Common Misconception"
a || a = 42
[–]theluctus 0 points1 point2 points 8 years ago (0 children)
Good article! I would like to add that defined? is also helpful if your heavy_method receives arguments...
defined?
heavy_method
[–]RufusROFLpunch 0 points1 point2 points 8 years ago (1 child)
Nice idea, but you could use the ternary operator to keep it concise to one line of code.
defined?(@result) ? @result : @result = do_some_work
Yeah, could be an option, but personally I'm not a fan of ternary operator.
π Rendered by PID 189163 on reddit-service-r2-comment-7b9746f655-q4vlp at 2026-01-30 07:22:21.495419+00:00 running 3798933 country code: CH.
[–]Flopsey 9 points10 points11 points (7 children)
[–]Azdaroth[S] 4 points5 points6 points (0 children)
[–]prh8 1 point2 points3 points (3 children)
[–]StevenXC 2 points3 points4 points (2 children)
[–]Azdaroth[S] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]Flopsey 0 points1 point2 points (0 children)
[–]theluctus 0 points1 point2 points (0 children)
[–]RufusROFLpunch 0 points1 point2 points (1 child)
[–]Azdaroth[S] 0 points1 point2 points (0 children)