Elegant Memoization with Ruby’s .tap Method | Alex MacArthur

If you're unfamiliar, the @results instance variable if will only be set if it's falsey. It's a nice way to ensure an expensive operation is performed only when it's needed and never more than once.For one-liners like this, it's straightforward. But sometimes, a little more complexity may require multiple lines of code, like if you were to fetch results from an external service. In that case, memoization isn't as elegant. That's where another neat Ruby feature can help retain that elegance. But first, let's flesh out a scenario.
Elegant Memoization with Ruby’s .tap Method | Alex MacArthur #ruby #rubydeveloper #rubyonrails https://rubyonrails.ba/single/elegant-memoization-with-ruby-s-tap-method-alex-macarthur

Nezir Zahirovic

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

related articles