From: zdennis Date: 2006-04-08T01:29:52+09:00 Subject: Re: autorequire.rb -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Depending on how smart you want it to be it will fail on stdlib's that don't follow a consistent downcase/spelling naming convention, ie: ostruct => OpenStruct It doesn't work with files which don't have a toplevel file to include, like 'net/ssh'. There is no 'net' file to load like there is with 'cgi' in 'cgi/session'. Although I can't think of a way OTOH to get around this with const_missing, since you'll never see the SSH part of Net::SSH because it will fail on Net. Zach ara.t.howard@noaa.gov wrote: > > > just hacked this out - can anyone see holes in it? > > > harp:~ > cat autorequire.rb > class Module > alias_method "const_missing!", "const_missing" > def const_missing c > lib4 = lambda{|k| k.to_s.downcase.gsub(%r/::/, File::SEPARATOR)} > lib = Object == self ? lib4[c] : lib4["#{ name }::#{ c }"] > begin; require lib; rescue LoadError; end > const_missing! c unless const_defined? c > const_get c > end > end > > if $0 == __FILE__ > p Object > p CGI > p CGI::Session > p PStore > p K > end > > > > harp:~ > ruby autorequire.rb > Object > CGI > CGI::Session > PStore > autorequire.rb:7:in `const_missing': uninitialized constant K > (NameError) > from autorequire.rb:17 > > > regards. > > > -a -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFENpRwMyx0fW1d8G0RAlDMAJ9GRGbwAEG3nbyH2t4FYdVx7WmVIwCdGAdb IN5Qznuop52w7ClovrY7mvM= =AW3I -----END PGP SIGNATURE-----