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>'
If you only need to load one file into IRB you can invoke it with irb -r ./your_file. rb if it is in the same directory. This automatically requires the file and allows you to work with it immediately. If you want to add more than just -r between each file, well that's what I do and it works.
We can just create a . rb file in the directory which you are currently working in using any text editor and type all the code in that and then use the command ruby filename. rb in the terminal, not in the irb, then it shows the output in irb.
None of these worked for me, but this did:
irb -I . >require 'file' => true
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With