This question mainly concerns the internal elements of Ruby, speed can be estimated by a simple standard.
What is the most efficient way to memoize the return value in ruby?
I always remembered the values ββwith:
def method @value ||= calculate_value end
But since it technically expands to:
@value = @value || calculate_value
I wonder about the effectiveness of re-executing the same task every time.
Would it be better?
def method @value ? @value : (@value = calculate_value) end
Also, does it change in different interpreters? MRI, Rubinius, etc.
ruby memoization
tompave
source share