Timeout, delegation - request

Hi all,

I’ve requested in the past that modules like “timeout” be wrapped in their
own module (well, ok, I said class, but I meant module).

Originally I requested this because the word “timeout” was a variable I like
to use from time to time (it’s fairly generic), but I occasionally would run
into trouble when I set an attr_accessor for it (as the timeout module’s
"timeout" method was then effectively overwritten).

Today I discovered a new reason for putting timeout into its own module -
delegation. If I delegate to an object that has a “timeout” method (such as
DBRC), then it forwards the call to that object, rather than calling the
timeout I expect. If I could do Timeout::timeout, I wouldn’t have this
issue (I don’t think I would, at least). I think this is the sort of
gotcha that could nail other people and it can be a pain to track down.

Please wrap standalone methods in their own modules!

Regards,

Dan

PS - there are others besides timeout.rb, but I can’t think of them at the
moment

[snip]

Please wrap standalone methods in their own modules!
[snip]

i agree with your reccomendation, but heres a hack fix…

----wrap.rb----

#!/usr/bin/env ruby

class Module
def wrap filename
f = filename.strip
absolute = (f[0] == File::SEPARATOR or f =~ /[1]+:/o)
lines = nil
errs = [“\n”]
if absolute
lines = IO.readlines f
else #relative
$:.each do |dir|
path = File.join dir, f
File.open (path) {|ios| lines = ios.gets nil } rescue errs << $!
break if lines
end
end
raise errs.join “\n” unless lines
lines.gsub! (%r{def\s+(\w+)}o) do |match|
method = $1
method =~ %r{^#{self}.}o ?
“def #{method}” :
“def #{self}.#{method}”
end
module_eval lines
end
end

module Timeout
wrap ‘timeout.rb’
end

Timeout::timeout nil do
puts ‘timeout wrapped’
end

module Foo
wrap ‘noexist.rb’
end

----wrap.rb----

eg/ruby > wrap.rb

timeout wrapped

…/wrap.rb:18:in `wrap’: (RuntimeError)

No such file or directory - “/usr/local/lib/ruby/site_ruby/1.6/noexist.rb”
No such file or directory - “/usr/local/lib/ruby/site_ruby/1.6/i686-linux/noexist.rb”
No such file or directory - “/usr/local/lib/ruby/site_ruby/noexist.rb”
No such file or directory - “/usr/local/lib/ruby/1.6/noexist.rb”
No such file or directory - “/usr/local/lib/ruby/1.6/i686-linux/noexist.rb”
No such file or directory - “./noexist.rb”
from ./wrap.rb:38

-a

···

On Fri, 10 Jan 2003, Berger, Daniel wrote:

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================


  1. \w\d ↩︎