Two main memoization patterns I’ve seen are the following
# Example 1 def momoize_it @the_thing ||= ThingClass::get() end # Example 2 def memoize_it return @the_thing if defined?(@the_thing) @the_thing = ThingClass::get() end
Example #1 is shorter, however if you stare at it hard enough you’ll realize if @the_thing is supposed to be false or a falsy value then it would never be memoized. Example #2 of checking if the variable has been defined is a more sure method of memoization, unless whats defined can’t be falsy.