Class: RubyMemoized::Memoizer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_memoized/memoizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/ruby_memoized/memoizer.rb', line 3

def context
  @context
end

#methodObject (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

#cacheObject



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