Does there exist a Ruby equivalent to PHP5 “magic” __autoload method
that spares the developer from detailing a list of “require” statements? http://us2.php.net/autoload
Or is this something that a homebrewed solution is necessary? Like
catching the exception and testing for file existence, etc.â?¦ or is there
a gem/extension featuring this functionality?
check out module Kernel - RDoc Documentation its
close
to what you want.
ActiveSupport is a gem which does autoloading, but it may be specific in
the
way it does it.
Does there exist a Ruby equivalent to PHP5 “magic” __autoload method
that spares the developer from detailing a list of “require” statements? PHP: Autoloading Classes - Manual
Or is this something that a homebrewed solution is necessary? Like
catching the exception and testing for file existence, etc.â?¦ or is there
a gem/extension featuring this functionality?
Does there exist a Ruby equivalent to PHP5 “magic” __autoload method
that spares the developer from detailing a list of “require”
statements? PHP: Autoloading Classes - Manual
Or is this something that a homebrewed solution is necessary? Like
catching the exception and testing for file existence, etc.? or is
there
a gem/extension featuring this functionality?
const_missing is a little more like PHP’s __autoload function.
The following, for example will attempt to just do: require in the event it can’t find the class.
def Object.const_missing name
require name.to_s.downcase
const_get(name)
end
I’ve never used it though, so anyone can feel free to correct me for
any gotchas that I’m not aware of.
-Mat
Module#autoload: class Module - RDoc Documentation
That’s not quite it. You need to call Module.autoload yourself. PHP’s
autoload is called by the interpreter to perform the inclusion. You
could use const_missing to get a similar functionality, though…
def Object.const_missing(name)
load name.to_lower + “.rb”
end
That’s got a few rough edges, though, to put it mildly…
You would probably want to use some metaprogramming to define
const_missing recursively on currently defined constants, and then
it’s pretty much the same as PHP’s __autoload().
Does there exist a Ruby equivalent to PHP5 “magic” __autoload method
that spares the developer from detailing a list of “require” statements? PHP: Autoloading Classes - Manual
Or is this something that a homebrewed solution is necessary? Like
catching the exception and testing for file existence, etc.â?¦ or is there
a gem/extension featuring this functionality?
def Object.const_missing elem
require elem.to_s
end
a = A.new
PS. I would advise NOT doing this kind of thing, thou. It can make
your code much harder to debug for other developers.
I do hope you know that there’s a lot of things in Ruby that makes a
developer’s debugging life quite hard, but the point is that it’s
convenient to have these sort of solutions.
const_missing is a little more like PHP’s __autoload function.
The following, for example will attempt to just do: require in the event it can’t find the class.
def Object.const_missing name
require name.to_s.downcase
const_get(name)
end
Well, th const_missing sollution does work, but only in scripts called
directly in shell by ‘ruby’ executable. When using it in script called
in browser i get:
[Tue Mar 20 16:56:49 2007] [error] mod_ruby: error in ruby
[Tue Mar 20 16:56:49 2007] [error] mod_ruby:
/home/services/httpd/html/TRON/ruby/index.rb:11: uninitialized constant
#Module:0xb6a6ea30::Test (NameError)
[Tue Mar 20 16:56:49 2007] [error] mod_ruby: from
/usr/lib/ruby/1.8/apache/ruby-run.rb:38:in load' [Tue Mar 20 16:56:49 2007] [error] mod_ruby: from /usr/lib/ruby/1.8/apache/ruby-run.rb:38:inhandler’
Got apache 2.2 with mod_ruby configured as follows: