I want to avoid reevaluation of a value in method call. Untill now, I was doing this:
def some_method
@some_method ||= begin
# lot\'s of code
end
end
I normally do it like in Agis answer or:
def filesize() @filesize ||=
calculate_filesize
end
BTW:
I often use this memoization technique:
def filesize() @_memo[:filesize] ||=
calculate_filesize
end
Which will allow you to later clear all memoized variables with one simple @_memo.clear
. The @_memo variable should be initialized like this Hash.new { |h, k| h[k] = Hash.new }
.
It gives you many of the adventages of using ActiveSupport::Memoize and similar meta programmed techniques which might be much slower.