“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:
- Cache expensive work
- 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
- 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 🙂