4

I have noticed several individuals comment on how their use of ||= to perform memoization was 'sloppy' but they did it anyway 'because they were in a rush'.

I also found ActiveSupport::Memoizable.

I'm lead to conclude that ||= has drawbacks.

So what are they?

Community
  • 1
  • 1
Alec Wenzowski
  • 3,878
  • 3
  • 25
  • 40
  • Hmm. The answer to this appears to be the same as http://stackoverflow.com/questions/963868/in-ruby-should-i-use-or-if-defined-for-memoization but it's a different question. Does that make it a duplicate? – Alec Wenzowski Aug 29 '11 at 23:20

2 Answers2

5

Well, the biggest reason I'm aware of is that you can't memoize nil or false values like that.

Wizard of Ogz
  • 12,543
  • 2
  • 41
  • 43
1

I suggest reading this:
When to use memoization in Ruby on Rails

And this:
http://www.railway.at/articles/2008/09/20/a-guide-to-memoization/

Community
  • 1
  • 1
Casper
  • 33,403
  • 4
  • 84
  • 79