Mark Chavez

Mark Chavez

Learning how to learn.

Ruby: Memoization Done Better

November 11, 2018

If you have written some Ruby code for a while, this is probably how proper memoization looks like to you.

def load_all_products
  @products ||= expensive_calculation
end

Simple! But not 100% correct.

What if expensive_calculation returns nil?

What if it returns false?

Since these are both falsey values, it’ll call expensive_calculation every time the method is called.

I’ve learned that the better way to do this is to check if the instance variable has a truthy value by using the defined? ruby keyword. Check the example below.

def load_all_products
  return @products if defined?(@products)
  @products = expensive_calculation
end

Happy reading!


Back to articles