Why isn't current directory on my Ruby path? [duplicate]
Asked Answered
U

3

35

Is there any reason why my present working directory is not on my Ruby path?

Consider:

~:499$ irb
ruby-1.9.2-p136 :002 > puts $:
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/x86_64-darwin10.6.0
 => nil 

This is really bothering me because require isn't working as I thought it would (although I'm a ruby nuby):

require 'some_file_that_I_know_darn_well_is_in_pwd.rb'

If I append '.' to the end, then the require works as I'd expect.

What am I missing?


UPDATE:

Arg! Now I'm getting a new problem. Consider:

ruby-1.9.2-p136 :010 > `ls`
 => "start.rb\n" 
ruby-1.9.2-p136 :011 > require_relative 'start'
LoadError: cannot infer basepath
    from (irb):11:in `require_relative'
    from (irb):11
    from /Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/bin/irb:16:in `<main>'

Now what's up?

Underground answered 11/2, 2011 at 4:12 Comment(0)
C
75

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
    
Confluent answered 11/2, 2011 at 4:59 Comment(7)
You brought me closer. Care to take a look at the updated question?Underground
@JohnBerryman Edited to match.Confluent
Well... that's solved the problem. Not as pretty of a solution as I had hoped for. I wonder if this is some oversight on part of the Ruby folks.Underground
The reason is quite simple: require_relative requires a file relative to the file that the require_relative call is in. Within IRB, the require_relative call isn't in any file, and thus cannot possibly work.Kittiekittiwake
@Jörg That explains the engineering "why", but does not explain the user "why" of "Hey, how about you just treat IRB as a file in the current directory, mmkay?"Confluent
@JohnBerryman I forgot to note that require './foo' works in IRB and files as well. Perhaps that's a prettier solution you were looking for.Confluent
@Confluent - I guess irb doesn't have to be pretty... thanks!Underground
H
3

You can use require_relative assuming it does what you need.

Hunsinger answered 11/2, 2011 at 4:25 Comment(0)
H
1

Make sure that the environment variable "RUBYLIB" is set with all directory paths where you will find custom *.rb code. It drove me nuts too.

Hardden answered 11/2, 2011 at 4:15 Comment(2)
That's not necessary. See @Phrogz' answer.Tuition
I wasn't aware of that one. It's a great answer (but I can't up vote it until tomorrow; out of votes). I'm going to change some of my code to try that one out. My way does work though. I have code that relies on it now, but I like his way and will try that out.Hardden

© 2022 - 2024 — McMap. All rights reserved.