Multi-line Memoization
Memoizing with Ruby’s ||=
is ugly when there’s more than one line of code. The kind folks over at Viget Labs shared this trick:
def foo
@foo ||= begin
arg1 = expensive_method_1
arg2 = expensive_method_2
expensive_method_3(arg1, arg2)
end
end
How did I not already know about this? What a readable solution.