Skip to main content

Benjamin Oakes

Photo of Ben Oakes

Hi, I'm Ben Oakes and this is my geek blog. Currently, I'm a Software Developer at Hedgeye. Previously, I was a Research Assistant in the Early Social Cognition Lab at Yale University and a student at the University of Iowa. I also organize NewHaven.rb. I do development with Ruby, JavaScript, SQL, HTML, and CSS. I have an amazing fiancée named Danielle Smith.

autoload in Ruby 1.8 and Rails

by Ben

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.

Comments

You can leave a comment, or trackback from your site. Article comments

Leave a comment