Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimize memoized method if there are no arguments

  • Loading branch information...
commit be0d235a3b82e72de49592913753a1f291a98f86 1 parent b2504f8
@josh josh authored
Showing with 17 additions and 7 deletions.
  1. +17 −7 activesupport/lib/active_support/memoizable.rb
View
24 activesupport/lib/active_support/memoizable.rb
@@ -29,14 +29,24 @@ def memoize(*symbols)
raise "Already memoized #{symbol}" if method_defined?(:#{original_method})
alias #{original_method} #{symbol}
- def #{symbol}(*args)
- #{memoized_ivar} ||= {}
- reload = args.pop if args.last == true || args.last == :reload
+ if instance_method(:#{symbol}).arity == 0
+ def #{symbol}(reload = false)
+ if !reload && defined? #{memoized_ivar}
+ #{memoized_ivar}
+ else
+ #{memoized_ivar} = #{original_method}.freeze
+ end
+ end
+ else
+ def #{symbol}(*args)
+ #{memoized_ivar} ||= {}
+ reload = args.pop if args.last == true || args.last == :reload
- if !reload && #{memoized_ivar} && #{memoized_ivar}.has_key?(args)
- #{memoized_ivar}[args]
- else
- #{memoized_ivar}[args] = #{original_method}(*args).freeze
+ if !reload && #{memoized_ivar} && #{memoized_ivar}.has_key?(args)
+ #{memoized_ivar}[args]
+ else
+ #{memoized_ivar}[args] = #{original_method}(*args).freeze
+ end
end
end
EOS
Please sign in to comment.
Something went wrong with that request. Please try again.