autoload in Ruby 1.8 and Rails
Just a PSA from a recent Ruby discussion:
Ruby’s autoload
works fine if you’re using it in a gem that’s loaded once (and need to use it on Ruby 1.8, vs. require_relative
in Ruby 1.9+), but if autoload
is used in a collection of Ruby classes or modules in a folder that’s in the Rails load path, then it’s probably not a good idea. You’ll get unexpected reloading behavior, at a minimum. That applies to submodules too, of course.
In general, you don’t want to mess with $LOAD_PATH
, which was a fairly common practice in 1.8. That’s largely gone away in 1.9 with the advent of require_relative
, but many gems need to be backwards compatible. In Ruby 1.8, you can always futz around with __FILE__
, but that gets messy as well.