Why Does Ruby 1.9.2 Remove "." from Load_Path, and What's the Alternative

Why does Ruby 1.9.2 remove . from LOAD_PATH, and what's the alternative?

It was deemed a "security" risk.

You can get around it by using absolute paths

File.expand_path(__FILE__) et al

or doing

require './filename' (ironically).

or by using

require_relative 'filename'

or adding an "include" directory

ruby -I . ...

or the same, using irb;

$irb -I .

Why does Ruby 1.9.2 remove . from LOAD_PATH, and what's the alternative?

It was deemed a "security" risk.

You can get around it by using absolute paths

File.expand_path(__FILE__) et al

or doing

require './filename' (ironically).

or by using

require_relative 'filename'

or adding an "include" directory

ruby -I . ...

or the same, using irb;

$irb -I .

How does Ruby's $LOAD_PATH get affected in other files when changed in one file?

as other pointed out already LOAD_PATH is global.
You should not be relying on tricks like this though. In your case the right thing to do is to use require_relative

$LOAD_PATH does not include working directory?

$LOAD_PATH includes current directory in Ruby 1.8.7, but this behavior have been changed in 1.9.2. You can find possible explanations for the reasons behind this decision among answers to this question, but I think basic idea is that . in 1.8.7 stands for directory from which your code is executed and not the one where it is located. And in most cases you don't want that and . in your $LOAD_PATH is not reliable.

Using require_relative in 1.9.2 might be a good solution if you don't want to add . manually to your $LOAD_PATH everywhere. You can see here that what it does is just explicit expanding relative path. One thing to note is that it's not available in versions prior to 1.9.2 so it'll make your code incompatible with older rubies.

Best way to refactor after upgrade to 1.9.2

If you're talking about the current directory being missing from the search path when using require, try require_relative instead.


require_related was accepted into the core in this conversation. It's part of some security changes because $: no longer includes '.' in the search path.

It was announced as being part of the kernel in the 1.9.2 notes, in the Kernel section.

It's documented as an extension to Kernel but works like the regular require statement only it's relative to the calling script's directory.

How can I start my thin server using ruby? require': cannot load such file -- app (LoadError)

Change this line

require 'app'

to

require './app'

for more alternatives also see: Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?

load works on local path, require doesn't

If you provide just a filename to require, it will only look in the predefined $LOAD_PATH directories. However, if you provide a path with your filename, it should work:

puts 'This is the first (master) program file.'
require './loadee.rb'
puts 'And back again to the first file.'

You could also add your project's folder to the load path instead:

$LOAD_PATH.unshift File.dirname(__FILE__)
puts 'This is the first (master) program file.'
require 'loadee.rb'
puts 'And back again to the first file.'

And last, you could just use require_relative instead:

puts 'This is the first (master) program file.'
require_relative 'loadee.rb'
puts 'And back again to the first file.'

Why load file.rb works even though . is not in the load path?

See the documentation of Kernel#load clearly :

Loads and executes the Ruby program in the file filename. If the filename does not resolve to an absolute path, the file is searched for in the library directories listed in $:. If the optional wrap parameter is true, the loaded script will be executed under an anonymous module, protecting the calling program’s global namespace. In no circumstance will any local variables in the loaded file be propagated to the loading environment.

In case load 'second.rb' - second.rb has been internally resolved to the absolute path /Projects/test/second.rb,as your requiring file in the directory is same as required file directory. Nothing has been searched to the directories listed in$: for your case.

Just remember another way always
- The load method looks first in the current directory for files



Related Topics



Leave a reply



Submit