Ruby Memoization: ||= vs. defined? - Ruby on Rails and Ember.js consultant

In the majority of the Rails applications or even Ruby gems you can find a lot of use cases where you need to memoize a result of some computation for performance benefits and to not compute it again if this result has already been computed. Seems like doing the assignment to some instance variable with ||= operator is the most commonly used solution for this purpose, e.g. @result ||= do_some_heavy_computation. However, there are some cases where it might not produce the expected outome and you should actually use defined? operator instead.
Ruby Memoization: ||= vs. defined? - Ruby on Rails and Ember.js consultant #ruby #rubydeveloper #rubyonrails #ember.js https://rubyonrails.ba/single/ruby-memoization-vs-defined-ruby-on-rails-and-ember-js-consultant

Nezir Zahirovic

Contractor Ruby On Rails (8+ years) / MCPD .Net / C# / Asp.Net / CSS / SQL / (11 years)

related articles