1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Append a "_" to memoized instance variables

This commit is contained in:
Joshua Peek 2008-07-14 20:25:09 -05:00
parent 911c2c3813
commit 7f0346237e

View file

@ -7,15 +7,16 @@ module ActiveSupport
module ClassMethods
def memoize(symbol)
original_method = "_unmemoized_#{symbol}"
memoized_ivar = "@_memoized_#{symbol}"
raise "Already memoized #{symbol}" if instance_methods.map(&:to_s).include?(original_method)
alias_method original_method, symbol
class_eval <<-EOS, __FILE__, __LINE__
def #{symbol}
if defined? @#{symbol}
@#{symbol}
if defined? #{memoized_ivar}
#{memoized_ivar}
else
@#{symbol} = #{original_method}
#{memoized_ivar} = #{original_method}
end
end
EOS