D
David Masover
What if, instead of having CWD in the load path, we had
`File.dirname(__FILE__)`? Essentially, CWD of the file that the require
statement is originated through.
Close, but no. When I call 'require' in, say, lib/foo/bar.rb, I want "require
'foo/baz'" to work, but "require 'baz'" should not, and it especially should
not have lib/foo/baz.rb mask lib/baz.rb.
So, if this is going to be a default, I'd much rather it apply to the original
file only -- that is, the file in which Ilias' Kernel#executed? (or whatever)
returns true.
Then again, how many points of entry does a typical application have? I think
the worst-case structure might be something like:
bin/
bunch of scripts
lib/
bunch of library stuff that should be in the load path
Worst case, I might put an init.rb or something in the project root, so that
each script starts off with:
require_relative '../init'
Then, init.rb includes something like this:
$: << File.join(File.dirname(__FILE__), 'lib')
Ugly, but it's only in one place. Or, if you're likely to deal with enough
other pathnames to make it worthwhile:
require 'pathname'
$: << Pathname(__FILE__).parent.join('lib')
Slightly prettier, and __FILE__ is contained to one place inside your project.
I don't like it either, but it just doesn't seem that critical to me.
Then the old interface would work again,
it would be simpler to think about, security risks of CWD in the load path
would be addressed, and most importantly, I could write beautiful code
again (I die a little bit inside each time I write __FILE__).
I tend to agree that it reduces the security risks. I can see myself putting a
Ruby script in my PATH and running it as a command in an unsafe directory. I
can't often see a case where CWD in the path would be more useful than the
root of the project. I really can't see a case where I would drop a Ruby
script in an unsafe directory and then execute it -- it seems like if the
directory is unsafe, no attempt to make the script safe is going to get me
anywhere.
Still, as I said, it doesn't seem that critical to me. It seems like
require_relative, Pathname, and __FILE__ all do the trick.
Of course, I have no idea if it's even possible, and it's not immediately
clear to me how it would affect gems.
I'm not sure how it would affect them, but it seems like gems would need this
even less. With a gem, your $: is pretty much set up for you, so long as you
follow the conventions about putting scripts in bin and libraries in lib.
So it's again something that doesn't seem to matter much -- how many
applications do we have, in comparison to gems?