Clutch

Exploring Ruby memoization and the alternatives

Exploring Ruby memoization and the alternatives
Average rating: 0
(0 votes)

“I always knew that one day smalltalk would replace Java. I just didn’t know it would be called Ruby.” Now we know! There are so many things to tell about Ruby that nobody can stop us. Today let’s go into exploring Ruby memoization and the alternatives with us.

It’s common to use memoization in Ruby to check that instance variables in a method only get set once regardless of how many times the method is called.

Memoization is often used when deriving values from another state. Saving derived state to an instance variable is a form of caching. It is useful because it allows us to do some work only one time and then have it available via an instance variable. There’s something else to present in Ruby – constructors! You can almost always convert a memoized method into a simple reader by moving the logic to the constructor.

Memoization has two big advantages:

  1. Cache expensive work
  2. Delay expensive work via laziness

Most common uses of memoization in Ruby are premature optimization. But sometimes we have better solutions than memoization:

  • no caching
  • constructors
  • laziness
  • separate caching and calculation

Check this material for more details.

We’re excited to share the announcement that Amazon Transcribe Streaming Transcription Support in the AWS SDK for Ruby. Feel that excitement too 🙂

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

*By submitting this form you agree with our Privacy Policy.

Mailing & Legal Address

Syndicode Inc. 340 S Lemon Ave #3299, Walnut CA, 91789, USA

Visiting & Headquarters address
Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
Email info@syndicode.com
Phone (+1) 9035021111