Module: Pry::Config::Memoization::ClassMethods

Defined in:
lib/pry/config/memoization.rb

Instance Method Summary collapse

Instance Method Details

#def_memoized(method_table)

This method returns an undefined value.

Defines one or more methods who return a constant value after being called once.

Examples:

class Foo
  include Pry::Config::Memoization
  def_memoized({
    foo: proc {1+10},
    bar: proc{"aaa"<<"a"}
  })
end

Parameters:

  • method_table ({String => Proc})


22
23
24
25
26
27
28
29
30
# File 'lib/pry/config/memoization.rb', line 22

def def_memoized(method_table)
  method_table.each do |method_name, method|
    define_method(method_name) do
      method_table[method_name] = instance_eval(&method) if method_table[method_name].equal? method
      method_table[method_name]
    end
  end
  MEMOIZED_METHODS[self] |= method_table.keys
end