Neither ruby and nor irb can load .rb file in current directory
Asked Answered
L

7

30

I'm having a really noob problem with importing files in Ruby. I'm making a Ruby app in Windows XP. All the class files for the app are in "C:/Documents/Prgm/Surveyor_Ruby/lib". But when I require a file in another file, neither ruby nor irb can find the required file.

The current directory's contents:

C:\Documents\Prgm\Surveyor_Ruby\lib>dir
 Volume in drive C has no label.
 Volume Serial Number is AAAA-BBBB

 Directory of C:\Documents\Prgm\Surveyor_Ruby\lib

10/09/2010  06:32 PM    <DIR>          .
10/09/2010  06:32 PM    <DIR>          ..
10/08/2010  03:22 PM             5,462 main (commented).rb
10/08/2010  03:41 PM                92 question.rb
10/08/2010  09:06 PM             2,809 survey.rb
10/09/2010  06:25 PM               661 surveyor.rb
10/08/2010  01:39 PM             1,546 test.rb
               5 File(s)         10,570 bytes
               2 Dir(s)  40,255,045,632 bytes free

Confirmation that irb is in correct directory:

C:\Documents\Prgm\Surveyor_Ruby\lib>irb
irb(main):001:0> Dir.pwd
=> "C:/Documents/Prgm/Surveyor_Ruby/lib"

...yet irb can't load survey.rb:

irb(main):002:0> require 'survey'
LoadError: no such file to load -- survey
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from (irb):2
        from C:/Ruby192/bin/irb:12:in `<main>'
Linneman answered 9/10, 2010 at 22:39 Comment(1)
In addition to the top solution, require './survey' would also have worked. Basically your current directory isn't actually in the search path by default, so you have to either point ruby to it explicitly, or include it in the search path.Linneman
O
65

None of these worked for me, but this did:

irb -I .
>require 'file'
 => true
Overcharge answered 23/11, 2011 at 1:3 Comment(3)
Check also my solution, it's especially fine when you didn't fire up irb with -I . to set the pwd as the $LOAD_PATH directory.Thermonuclear
Wonderful feature of irb!Prohibitionist
@DanKozlowski More like, bad design of Ruby! In Python, the current directory is added to PATH by default.Psychosomatics
T
11
require './hede'

or

require_relative 'hede'

This works for me in both Ruby (1.9.3) and JRuby (1.7.x) on linux. I haven't tested it on windows.

Tremolant answered 18/3, 2013 at 9:9 Comment(0)
T
10

How about this command? A little cumbersome to write but really clean and it should always work:

➜ $ irb
> require "#{Dir.pwd}/file_to_load.rb"
=> true 
Thermonuclear answered 6/10, 2012 at 11:38 Comment(0)
F
8

Noticed the same behavior but my linux roots had me try:.\file.rb and it loaded into the irb. Try explicitly declaring the current directory.

Fleenor answered 10/10, 2010 at 12:43 Comment(1)
This only works if the current directory happens to be the one that both the files are in.Susie
F
3

it's damn dirty, but you can always do at the very first line:

$: << '.'

and off you go with pwd'ed require. It's quite useful for interactive/creative testing with IRB

Feoff answered 29/5, 2011 at 10:15 Comment(1)
Thanks for this. I have no idea why require wasn't working for me.Weigh
H
2

I believe both of the previous posts are correct, just for different uses. In IRB use an absolute path with require, with a file you can also use require with an absolute path, or use require_relative.

Humanitarian answered 10/10, 2010 at 2:14 Comment(0)
E
1

If you're trying to do this with rvmsudo, I found this worked for me:

rvmsudo irb -I '/Absolute/path/to/your/project'
Epinasty answered 12/9, 2012 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.