Hi folks,

I've been trying to improve my metaprogramming skills the past two
weeks and came up with the following library which caches specific
method calls transparently so you don't have to explicitly save the
result. Is it interesting to anyone out there? If so I'll gemify it
and release it. A quick example:

require 'cachablemethods'

def timer(start,result,finish)
  puts "Computed value #{result} in #{finish - start} seconds"
end

class Foo
  include CachableMethods
  def bar(num)
    sleep(5)
    rand(num)
  end

  def baz
    sleep(5)
    return yield(rand(1000))
  end

  def baq
    sleep(5)
    rand(1000)
  end
  cache_method :bar,:baz,:baq
end

foo = Foo.new

puts "Initial calls cached"
timer(Time.new,foo.bar(1000),Time.new)
timer(Time.new,foo.baz{|n| n*2},Time.new)
timer(Time.new,foo.baq,Time.new)

puts "Calling method without parameters/blocks return cached result"
timer(Time.new,foo.bar,Time.new)
timer(Time.new,foo.baz,Time.new)
timer(Time.new,foo.baq,Time.new)

puts "Calling methods with parameters/blocks or ending with ! if it
takes neither recaches values"
timer(Time.new,foo.bar(1000),Time.new)
timer(Time.new,foo.baz{|n| n*3},Time.new)
timer(Time.new,foo.baq!,Time.new)

farrel@nicodemus ~/Projects/cachable/lib $ ruby test.rb
Initial calls cached
Computed value 516 in 4.998986 seconds
Computed value 1848 in 4.999224 seconds
Computed value 200 in 4.999236 seconds
Calling method without parameters/blocks return cached result
Computed value 516 in 3.6e-05 seconds
Computed value 1848 in 2.0e-05 seconds
Computed value 200 in 2.1e-05 seconds
Calling methods with parameters/blocks or ending with ! if it takes
neither recaches values
Computed value 935 in 4.998461 seconds
Computed value 648 in 4.999118 seconds
Computed value 83 in 4.99987 seconds

Farrel