Class: RubyMemoized::Memoizer
- Inherits:
-
Object
- Object
- RubyMemoized::Memoizer
- Defined in:
- lib/ruby_memoized/memoizer.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
Instance Method Summary collapse
- #cache ⇒ Object
- #call(*args, &block) ⇒ Object
-
#initialize(context, method) ⇒ Memoizer
constructor
A new instance of Memoizer.
Constructor Details
#initialize(context, method) ⇒ Memoizer
Returns a new instance of Memoizer.
5 6 7 8 |
# File 'lib/ruby_memoized/memoizer.rb', line 5 def initialize(context, method) @context = context @method = method end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
3 4 5 |
# File 'lib/ruby_memoized/memoizer.rb', line 3 def context @context end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
3 4 5 |
# File 'lib/ruby_memoized/memoizer.rb', line 3 def method @method end |
Instance Method Details
#cache ⇒ Object
15 16 17 |
# File 'lib/ruby_memoized/memoizer.rb', line 15 def cache @cache ||= {} end |
#call(*args, &block) ⇒ Object
10 11 12 13 |
# File 'lib/ruby_memoized/memoizer.rb', line 10 def call(*args, &block) return cache[[args, block]] if cache.has_key?([args, block]) cache[[args, block]] = context.send(method, *args, &block) end |