Why Isn't Current Directory on My Ruby Path

Why isn't current directory on my Ruby path?

In Ruby 1.9.2 the Powers that Be introduced an explicit change so that the working directory is no longer in the Ruby path. I thought it was the Apocalypse and a terrible thing, until I learned about require_relative. My apps tend to look like this:

require 'some_gem'
require 'another_gem'
require_relative 'lib/init'

And then lib/init.rb can have:

require_relative 'lib1' # this is lib/lib1.rb
require_relative 'lib2' # this is lib/lib2.rb

It's the bees knees, and solves all sorts of problems I used to have with requiring the same file from different working directories.

Edit: Unfortunately (for reasons I don't know and haven't looked into) require_relative doesn't work specifically in irb. For this you can:

  1. do what you initially described: either $: << '.' or $:.unshift '.', or
  2. you can use load 'myfile.rb' or require './myfile' instead:

    irb(main):001:0> Dir['*.rb']
    => ["a.rb", "bar.rb", "foo.rb", "prime.rb", "tmp.rb"]

    irb(main):002:0> require 'a'
    LoadError: no such file to load -- a
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from (irb):2
    from /usr/local/bin/irb:12:in `<main>'

    irb(main):003:0> require_relative 'a'
    LoadError: cannot infer basepath
    from (irb):3:in `require_relative'
    from (irb):3
    from /usr/local/bin/irb:12:in `<main>'

    irb(main):004:0> load 'a.rb'
    a
    => true

    irb(main):005:0> require './a'
    a
    => true

Get the file path based on the current working directory?

You should be able to just do something like follows.

absolute_path.split(Dir.pwd.to_s)[1].sub('/','')

This gets a 2 element array where the second element is the remaining portion of the path you want. The sub then strips off the prefixing forward slash.

Can a Ruby script tell what directory it’s in?

For newer versions of Ruby, try:

  • __dir__

For older versions of Ruby (< 2.0), the script being run can be found using:

  • File.dirname(__FILE__) - relative path; or
  • File.expand_path(File.dirname(__FILE__)) - the absolute path.

Note: Using __dir__ will return the script path even after a call to Dir.chdir; whereas, using the older syntax may not return the path to the script.

Ruby, including module in current directory

You use require to load a file, not include. :-)

First, you have to require the file containing the module you want to include. Then you can include the module.

If you're using Rails, it has some magic to automagically require the right file first. But otherwise, you have to do it yourself.

$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.

Ruby on Windows path for requires in dir not working

Ok, I understand now since 1.9.2 for "Security" reasons they don't allow require to work like that anymore. The neatest way I found to solve it strangely was to put './' in front of every require.

e.g.

require "./myfile.rb"


Related Topics



Leave a reply



Submit