0

Possible Duplicate:
Why does Ruby 1.9.2 remove “.” from LOAD_PATH, and what's the alternative?
Require command not working within bash irb on Snow Leopard
Ruby require call fails on custom code

I'm running:

  • OS X 10.6.8
  • RVM 1.12.3 with Ruby 1.9.3

Let's say I have the following files in a directory:

a.rb

require 'b'
puts message_from_b

b.rb

def message_from_b
    "foo"
end

I get the following LoadError upon running a.rb:

/Users/Alex/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- b (LoadError)

However, if I replace line 1 of a.rb with require './b', it works fine.

When looking at other people's Ruby code, it seems that they don't need to specify the current directory for it to require a file in the same directory.

So, why do I have to do that? Is it something to do with my RVM install?

Community
  • 1
  • 1
Alex Coplan
  • 13,211
  • 19
  • 77
  • 138
  • You might want to read http://stackoverflow.com/questions/2900370/why-does-ruby-1-9-2-remove-from-load-path-and-whats-the-alternative for a discussion of why this doesn't work. – kfb Jun 08 '12 at 11:37

1 Answers1

2

File b.rb is not in your load path. Either add b.rb's directory to your load path

$LOAD_PATH.unshift(File.dirname(__FILE__)) # assuming it's the current directory

or use

require_relative 'b'
Stefan
  • 109,145
  • 14
  • 143
  • 218