-
Get a monthly update on best practices for delivering successful software.
Recently, on a RoR project, I needed to write a utility in lib folder and corresponding test in test/* folder. I had assumed that anything in folders/subfolders of lib would automatically get loaded as part of environment load. For some reason, it wasn't loading my utility file when I ran my test. The obvious solution would have been to require that file specifically in my test file but I was curious to see if there is a way to figure how rails does its loading magic. Armed with the information that any method that does not act on any object are considered part of "Object" class, I wrote something like this:
class Object
def require(ruby_file)
p "Requiring #{ruby_file} ..."
super
end
end
I put this at the top of my test file, which was the very first file to get loaded, even before the usual line requiring 'test_helper'.
And Woila !!. The next time I ran the test, it showed following:
"Requiring rubygems ..." "Requiring mechanize ..." "Requiring fileutils ..." "Requiring etc ..." "Requiring fileutils ..." "Requiring www/mechanize ..." "Requiring net/http ..." "Requiring net/protocol ..." "Requiring socket ..." "Requiring timeout ..." ...
Next, what I actually needed is to override "load" so that I see only the files as they are loaded as opposed to files being 'require'd (See require vs. load). So, I tried this:
class Object
def load(ruby_file)
p "Loading #{ruby_file} ..."
super
end
end
For some reason that I have yet to figure out, it didn't work. Do you know why?
Related posts:
Topics: Ruby on Rails
use Kernel.load and Kernel.require
Comment by Delegitimizer, Monday, August 11, 2008 @ 1:56 pm