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:
parent
911c2c3813
commit
7f0346237e
1 changed files with 4 additions and 3 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue