def hash_cache(method_name, options = {})
selector = options[:as] || "#{method_name}_cache"
method = self.instance_method(method_name)
args = []
code = "def #{selector}(); @#{selector} ||= "
(1..method.arity).each do |n|
args << "v#{n}"
code << "Hash.new {|h#{n}, v#{n}| h#{n}[v#{n}] = "
end
code << "#{method_name}(#{args * ', '}) #{'}' * method.arity} end"
if caller[0] && /^(.*):(\d+)$/ =~ caller[0]
file, line_number = $1, $2.to_i
else
file, line_number = __FILE__, __LINE__
end
class_eval code, file, line_number
end