All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is memoization in Ruby?

Post cover: What is memoization in Ruby?
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
Memoization - this is when Ruby "remembers" the result of the calculation so as not to count again. Used for optimization - especially when the method is called many times.

How @x works ||= ...

@x ||= fetch_x
means:
If @x doesn't matter yet (ie nil or false), then execute fetch_x and save the result to @x. Otherwise, just return the already saved one.

Difference with simple assignment

@x = fetch_x # executes fetch_x EVERY time
@x ||= fetch_x # only performs fetch_x FIRST time
For example:
def expensive
  puts "Calculating..."
  42
end

def answer
  @answer ||= expensive
end

answer # => Calculating... 42
answer # => 42 (no calculation for the second time!)
But careful:
||= will not work if the value can be false:
@flag ||= true
# if @flag = false, Ruby will consider it "empty" and rewrite it to true
If you need to save even false, it is better to write:
@flag = true if @flag.nil?

This post doesn't have any additions from the author yet.

What are Proc and Lambda in Ruby?
28 Oct 15:57

What are Proc and Lambda in Ruby?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What happens if you call [1, 2, 3].map(&Person)
29 Oct 17:54

What happens if you call [1, 2, 3].map(&Person)

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Singleton class (eigenclass) in Ruby: what it is and why it is needed
29 Oct 18:29

Singleton class (eigenclass) in Ruby: what it is and why it is needed

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
==, equal?, eql?, === in Ruby: what is checked and when to use
29 Oct 20:47

==, equal?, eql?, === in Ruby: what is checked and when to use

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Include, Extend, Prepend in Ruby: how they work and what's the difference
29 Oct 21:20

Include, Extend, Prepend in Ruby: how they work and what's the difference

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
module_function in Ruby: When module methods are available as modular and as functions
29 Oct 21:53

module_function in Ruby: When module methods are available as modular and as functions

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
is_a?, kind_of?, instance_of? — how does Ruby check object type?
30 Oct 19:55

is_a?, kind_of?, instance_of? — how does Ruby check object type?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
&& vs and — difference in Ruby that can break your code
30 Oct 20:23

&& vs and — difference in Ruby that can break your code

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
The variables in Ruby are @, @@ and class instance variable
30 Oct 20:54

The variables in Ruby are @, @@ and class instance variable

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
The difference between blank?, present?, empty? but nil? in Ruby
30 Oct 21:06

The difference between blank?, present?, empty? but nil? in Ruby

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What Middleware is in Ruby on Rails and when it is used
04 Nov 10:39

What Middleware is in Ruby on Rails and when it is used

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is Vanilla Rails approach?
14 Nov 16:48

What is Vanilla Rails approach?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska