Can someone please explain what's going on here. No matter what I try,
every time I call require() with a relative path, it comes back with
"LoadError: no such file to load". Yet, at the same time, load() works
perfectly fine with the exact same relative path. Require() doesn't seem
to have a problem with absolute paths, in fact this bit of code works
flawlessly...
require File.expand_path('subfolder/file.rb')
...but the follow code fails with the LoadError mentioned earlier...
require 'subfolder/file.rb'
I've got RubyGem's installed, so I thought maybe it's override of the
require() method was causing havoc, so I tried gem_original_require()
with the exact same result, LoadError.
I'm getting this problem from both the "irb" and the "ruby" executables.
Checking Dir.pwd reveals that indeed, the current directory is as
expected/intended.
Can someone explain to me why require() isn't working with relative
paths? I'm completely baffled and don't know where to go from here.
Cheers
···
--
Posted via http://www.ruby-forum.com/.
What version of ruby are you using?
In 1.9 the current directory (.) is no longer included in the load
path ($LOAD_PATH, or $:)
Regards,
Ammar
···
On Thu, Oct 7, 2010 at 8:19 AM, Tom Wardrop <tom@tomwardrop.com> wrote:
Can someone explain to me why require() isn't working with relative
paths? I'm completely baffled and don't know where to go from here.
Ammar Ali wrote:
What version of ruby are you using?
In 1.9 the current directory (.) is no longer included in the load
path ($LOAD_PATH, or $:)
Regards,
Ammar
I'm on 1.9.2. This now raises a number of questions...
1) How come the load() method isn't affected by this?
2) What is the logic behind not allowing files to be referenced with a
relative path?
3) What is the correct way for referencing files relatively?
Thanks for your help thus far!
···
--
Posted via http://www.ruby-forum.com/\.
I'm on 1.9.2. This now raises a number of questions...
1) How come the load() method isn't affected by this?
I'm not sure, but I guess (hope someone else will chime in) it is
because load needs a full file name, while require tries different
extensions (.rb, .o, .so, .dl, .dyld, maybe others) potentially
loading something you did not intend.
2) What is the logic behind not allowing files to be referenced with a
relative path?
Only via require, as you discovered. It's a security measure. Please
see: http://redmine.ruby-lang.org/issues/show/1733
3) What is the correct way for referencing files relatively?
The link above mentions two possible ways which are useful on the
command line or from scripts (-I and RUBYLIB). You can also use
__FILE__ in combination File.expand_path to require files relatively
from a given file:
require File.expand_path( 'some/file', __FILE__)
Regards,
Ammar
···
On Thu, Oct 7, 2010 at 1:37 PM, Tom Wardrop <tom@tomwardrop.com> wrote:
In 1.9.x, there's require_relative for that purpose.
Vale,
Marvin
···
Am 07.10.2010 14:11, schrieb Ammar Ali:
The link above mentions two possible ways which are useful on the
command line or from scripts (-I and RUBYLIB). You can also use
__FILE__ in combination File.expand_path to require files relatively
from a given file:
require File.expand_path( 'some/file', __FILE__)
I keep forgetting about that one. Probably from avoiding it enough
times for code that needs to run on 1.8.
Regards,
Ammar
···
On Thu, Oct 7, 2010 at 6:28 PM, Quintus <sutniuq@gmx.net> wrote:
In 1.9.x, there's require_relative for that purpose.