Elegant Memoization with Ruby’s .tap Method | Alex MacArthur
21-Apr-2023 625
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 #Elegant #Memoization #Ruby’s #Method #MacArthur https://rubyonrails.ba/link/elegant-memoization-with-ruby-s-tap-method-alex-macarthur