4

I have a file in a directory test/. The file is "test.rb".

When I start IRB and type:

require 'test'
require_relative 'test'

It returns:

LoadError: cannot load such file -- test

and:

LoadError: cannot infer basepath

Is my Ruby messed up?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Test Test
  • 2,831
  • 8
  • 44
  • 64

2 Answers2

11

Try require './test'

I think newer versions of Ruby removed the current directory from the Ruby path.

Edit:

Also, check out the answer to "Why isn't current directory on my Ruby path?" for an explanation why require_relative doesn't work in irb.

Community
  • 1
  • 1
Zajn
  • 4,078
  • 24
  • 39
  • The explanation is in the comments on that answer, by the way. – Zajn Jan 10 '13 at 12:16
  • i.e. `require` works by running this in irb first: `$: << '.'` for me. However, a nested `require '...'` inside the included file still relative to the original working directory, instead of the included file. – Johnny Wong Jun 21 '16 at 10:30
0

The file which is to be required should be present in the same directory as interactive ruby is present (for windows). For eg, I have installed ruby in C:\ . Hence, my interactive ruby is in C:\Ruby22\bin. So, paste the required file and type require './file' in the 'interactive ruby'.

  • You may want to mention why the directory containing the file matters, specifically how the path variable interacts with interactive ruby. – Matt Habel Jul 10 '15 at 16:31