__DIR_DIR_DUR__

| | Comments (5)

Something I'll never understand...

require File.expand_path(File.dirname(__FILE__) + "/../config/environment")

vs

require 'config/environment'

The choice seems obvious to me.

5 Comments

The second one requires the $LOAD_PATH is setup properly right? So it seems like the only reason the former is better is that if it's the first require, it saves a line to setup that.

Maybe it would still be better to just modify the loadpath then require "config/environment" though.

-Bryan

Yes, but we ALWAYS expect that load path is set up correctly:

% ruby -e '$:.clear; require "time"'
-e:1:in `require': no such file to load -- time (LoadError)
     from -e:1

I don't see the point of "saving a line to setup" if it means that I have tons of unreadable crap all over my code. I'd rather require autotest/rake to set things up correctly and let ruby do its thing normally.

Greetings,

The idea is that the load path is only 'setup properly' if you're running a script when your current working directory is the root of the Rails app (in that the load path contains '.'). If you're trying to run it with your cwd in an arbitrary location, the load path won't contain the Rails app's root directory, and therefore won't be set up properly.

Not sure what else to say... If you want to be able to run a test, say 'user_test.rb' from an arbitrary cwd via:

ruby ~/my_apps/app/test/unit/user_test.rb

then you really have to have something which disambiguates the path to find the appropriate environment.rb. Try it with your require, and you'll quickly get:

no such file to load -- config/environment (LoadError)

It may not be something YOU ever do, but it's not impossible to understand.

-- Morgan

Morgan,

You've solidly wandered into the world of "what ifs" that so many developers get trapped in. You're describing a non-issue as if it is a valid problem that we encounter on a daily basis. I say if you're experiencing this often, your work patterns are simply wrong. I've NEVER had this problem. Rake always fixes your cwd, autotest doesn't have this problem, and I've never had a problem either adding -Ilib when I run an individual test. I should add that the last scenario is increasingly rare because I use autotest. Rake even has a filter variable so you can easily run a single test file.

Are you REALLY running tests from an arbitrary directory? I never am. I think it is a bogus scenario that you are OR that you're not being as effective as you could be.

So yes, I find this impossible to understand... in that I refuse to understand a mindset bent on making things more complicated for mentabatory reasons.

Definitely doesn't make sense for config/environment in Rails.

One reason people might do this is that they know about that cute behavior of #require where it'll load the same code twice if you refer to the same file using two different strings. That is, they think that by always doing a full File.expand_path every time you use #require, you're following some sort of best-practice about making sure not to load something twice.

Still doesn't make much sense--unless you're programming in a vacuum--since all the other code in a typical program (e.g. the stdlib) doesn't do this.

Leave a comment

About this Entry

This page contains a single entry by zenspider published on July 20, 2009 4:05 PM.

hoe version 2.3.2 has been released! was the previous entry in this blog.

rubyforge version 1.0.4 has been released! is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.32-en