module Lemo::Memo::ClassMethods

Public Instance Methods

ivar_from( maybe_meth ) click to toggle source

provide a legal ivar name from a method name. instance variables can't have ? ! and other punctuation. Which isn't handled. Obviously.

# File lib/lemo/memo.rb, line 16
def ivar_from( maybe_meth )
  :"@_memo_#{maybe_meth.to_s.tr ILLEGAL_IVAR_CHARS,'pi'}"
end
lemo( meth ) click to toggle source
# File lib/lemo/memo.rb, line 20
def lemo( meth )
  unbound_previous_method = instance_method meth

  # still doesn't prevent memoisation of methods with an implicit block
  unless unbound_previous_method.parameters.empty?
    raise ArgumentError, "can't memo #{meth} with parameters"
  end

  memoed_methods[meth] = unbound_previous_method
  ivar = ivar_from meth

  define_method meth do
    # This gets executed on every call to meth, so make it fast.
    if instance_variable_defined? ivar
      instance_variable_get ivar
    else
      # bind the saved method to this instance, call the result ...
      to_memo = unbound_previous_method.bind( self ).call
      # ... memo it and return value
      instance_variable_set ivar, to_memo
    end
  end

  meth
end
Also aliased as: memo
memo( meth )

for all the things using memo already

Alias for: lemo
memoed_methods() click to toggle source
# File lib/lemo/memo.rb, line 10
def memoed_methods
  @memoed_methods ||= {}
end