From: Daniel Berger Date: 2005-09-10T05:17:28+09:00 Subject: Re: memoize nobu.nokada@softhome.net wrote: > Hi, > > At Wed, 7 Sep 2005 14:41:28 +0900, > horndude77@gmail.com wrote in [ruby-talk:155156]: > >>- Is there a way to do this without an eval to speed it up? >>- Make this work on the class level. > > > module Kernel > def memoize(name) > m = method(name) > cache = {} > (class << self; self; end).class_eval do > define_method(name) do |*args| > cache[args] ||= m.call(*args) > end > end > end > end I'm not sure I follow: def fib(n) return n if n < 2 fib(n-1) + fib(n-2) end memoize(fib) fibonacci.rb:20:in `fib': wrong number of arguments (0 for 1) (ArgumentError) from fibonacci.rb:20 Regards, Dan