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