Module: Memoize::ClassMethods

Defined in:
lib/dpl/helper/memoize.rb

Instance Method Summary collapse

Instance Method Details

#memoize(name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dpl/helper/memoize.rb', line 7

def memoize(name)
  ivar = :"@#{name.to_s.sub('?', '_predicate')}"
  prepend Module.new {
    define_method(name) do |*args|
      raise ArgsError, 'cannot pass arguments to memoized method %p' % name unless args.empty?
      return instance_variable_get(ivar) if instance_variable_defined?(ivar)

      instance_variable_set(ivar, super())
    end
  }
end