On 12/5/05, Brian Buckley <briankbuckley / gmail.com> wrote:
>
> Is there anything already written that does this type of thing?
Try this out Brian:
require 'timeout'
class TooMuchTimeException < Exception;end
class Class
def time_limit(hash)
hash.each do |name, limit|
m = instance_method(name)
define_method(name) do
timeout(limit, TooMuchTimeException) do
m.bind(self).call
end
end
end
end
end
class Foo
def calc1
1
end
def calc2
sleep(7)
2
end
time_limit :calc1 => 20, :calc2 => 5
end
f = Foo.new
p f.calc1
p f.calc2