Neither ruby and nor irb can load .rb file in current directory - ruby

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>'

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.

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

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.

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

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.

If you're trying to do this with rvmsudo, I found this worked for me:
rvmsudo irb -I '/Absolute/path/to/your/project'

Related

ruby require command not loading correctly

First post, "Hello World"
I am working through the lynda videos on Ruby and am just getting to the part of requiring content from .rb files in irb. An example patch we made is named contact_info.rb and from irb I am trying to require that file. When executed it comes back with the attached below.
Some light googling made it seem like this is maybe a yosemite issue (running 10.10.3.), but I'm not sure how to troubleshoot.
Thanks all
irb(main):006:0> require contact_info.rb
LoadError: cannot load such file -- contact_info.rb
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in 'require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in 'require'
from (irb):6
from /usr/bin/irb:12:in '<main>'
you can use require_relative 'contact_info'.
Assuming the file is in your current directory, type this in your command line:
irb -r './contact_info.rb'
First, note that with any require statement, you omit the file extension:
require 'contact_info'
When you require a file, ruby only looks in certain directories on your computer for the file. You can see which directories those are by running the following code:
p $LOAD_PATH
In ruby 1.8.7, the $LOAD_PATH array included ".", or the current directory, which means your code would have worked. But, including the current directory in $LOAD_PATH was deemed a security risk, so now you have to do something different:
1) One option is to use a relative path for the file you specify in the require statement:
require './contact_info'
The path is relative to the current directory. That works fine if you have this structure:
/some/dir/
your_prog.rb
contact_info.rb
And you switch directories to /some/dir and then run your_prog.rb:
~$ cd /some/dir
/some/dir$ ruby my_prog.rb
The require statement works--no problems. However, what if you do this:
/some/dir$ cd ..
/some$ ruby ./dir/your_prog.rb
Now, the current directory is /some, and require './contact_info' tells ruby to look in the /some directory for contact_info.rb--but it isn't there, so you will get the error:
`require': cannot load such file -- ./contact_info.rb (LoadError)
2) To cure that problem, ruby added require_relative. Paths specified with require_relative are relative to the location of the file that contains the require_relative statement. As a result, the statement:
#your_prog.rb:
require_relative './contact_info'
...will look in the directory containing your_prog.rb for the file contact_info.rb. Now, doing this:
/some$ ruby ./dir/your_prog.rb
will work fine. And in fact, in the require_relative you don't even have to write the ./ in the path:
#your_prog.rb:
require_relative 'contact_info' #Look for contact_info.rb in the same
#directory that contains this file
require_relative '../contact_info' #Look for contact_info.rb one directory
#above the directory that contains this file
I am working through the lynda videos on Ruby and am just getting to
the part of requiring content from .rb files in irb.
In my opinion, it's not a good idea to use irb for much of anything. A better option is to create a couple of files called 1.rb, 2.rb, 3.rb, and do your coding in those files.

Require not able to find ruby file

I am an absolute beginner in Ruby. I created a small ruby file, and it runs well when I run the command ruby "methods.rb". That means I am in the correct directory.
But when I launch irb and run the command require "methods.rb", I get the following response:
LoadError: cannot load such file -- methods.rb
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:53:in `require'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:53:in `require'
from (irb):1
from /usr/local/rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
Ruby doesn't add the current path to the load path by default.
From irb, you can try require "./methods.rb" instead.
I do have a ruby file called so.rb in the directory /home/kirti/Ruby. So first from IRB I would change my current working directory using Dir#chdir method. Then I would call #load or #require method. My so.rb file contains only p hello line.
I would go this way :
>> Dir.pwd
=> "/home/kirti"
>> Dir.chdir("/home/kirti/Ruby")
=> 0
>> Dir.pwd
=> "/home/kirti/Ruby"
>> load 'so.rb'
"hello"
=> true
>> require './so.rb'
"hello"
=> true
To add the directory you are executing the ruby script from to the load path use:
$LOAD_PATH.unshift( File.join( File.dirname(__FILE__), '' ) )
or if you have put your dependencies in 'subdir' of the current directory:
$LOAD_PATH.unshift( File.join( File.dirname(__FILE__), 'subdir' ) )
If you are going to load things in IRB that are in your current directory, you can do:
irb -I.
Note the 'dot' there, indicating current directory.
If you are exploring and making changes in that file, while you are in IRB, use load rather than `require as load lets you load your changes, and require will only allow the file to be required once. This means you will not need to exit IRB to see how your changes are being affected.
To find out what options you have for IRB, you can do irb --help which is good to do if you are learning the tool.

Can't read a file that contains a question mark in name

I have a file with a name something like: /path/to/file/dir with spaces/mytiff-?.tif. In irb I try both the following:
open("/path/to/file/dir with spaces/mytiff-?.tif", 'rb')
File.open("/path/to/file/dir with spaces/mytiff-?.tif", 'rb')
And I'm getting an error:
Errno::ENOENT: No such file or directory.
I've also tried replacing the ? with \? in the string.
I'm using ruby 2.0.0. File is network drive (CIFS), client is a mac (lion).
Update
Thanks for the help. The problem is still not solved.
$ ls "/Volumes/rmrpp/MLP Library/Bridgland 1927 - North Saskatchewan/Stations/420/Repeat Masters/"
HIG2008_B27_420-100.tif HIG2008_B27_420-90.tif HIG2008_B27_420-92.tif HIG2008_B27_420-94.tif HIG2008_B27_420-96.tif HIG2008_B27_420-98.tif HIG2008_B27_420-?.tif
HIG2008_B27_420-101.tif HIG2008_B27_420-91.tif HIG2008_B27_420-93.tif HIG2008_B27_420-95.tif HIG2008_B27_420-97.tif HIG2008_B27_420-99.tif
$ irb
1.9.3p194 :001 > t = File.open("/Volumes/rmrpp/MLP Library/Bridgland 1927 - North Saskatchewan/Stations/420/Repeat Masters/HIG2008_B27_420-?.tif",'rb'); nil
Errno::ENOENT: No such file or directory - /Volumes/rmrpp/MLP Library/Bridgland 1927 - North Saskatchewan/Stations/420/Repeat Masters/HIG2008_B27_420-?.tif
from (irb):1:in `initialize'
from (irb):1:in `open'
from (irb):1
from /Users/mlp/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
1.9.3p194 :002 > t = File.open("/Volumes/rmrpp/MLP Library/Bridgland 1927 - North Saskatchewan/Stations/420/Repeat Masters/HIG2008_B27_420-98.tif",'rb'); nil
=> nil
1.9.3p194 :003 > t.size
=> 50124180
As you can see, the file exists (I'm listing it in bash). I go into irb and try to read the file as a binary. The file path is quoted. It fails. I try another file from the same directory and it works.
I think #mu_is_too_short is correct. Maybe the ? isn't a question mark at all. The only viable solution I can think of is to manually rename the files with the mystery question mark. Any other ideas?
As long as the file exists, there is no problem opening a file like this:
folder_file = './temp directory/file-?.txt'
File.open(folder_file, 'r').close
This works fine.
See Tin Man's suggestion about how to actually open the .tif file.
As you can see, I created the following file myster-file_char?.txt
$: ls
get_listing.rb myster-file_char?.txt
You can get this stored in a variable, though I only use Dir.glob here to view it.
puts Dir.glob("**.*").inspect
Everything is now revealed in what character it is. Though I should be able to use it directly as is to open and edit the file in Ruby. Though like The Tin Man stated, you are asking about a binary file.
$: ruby get_listing.rb
["myster-file_char\u0016.txt", "get_listing.rb"]
The character that is actually there is a CTRLV. Hope this finally helps to unravel the mystery.

How do I require a Ruby file?

I have a file called "go.rb" that contains:
require 'turboname'
dictionary = Turboname::Random.new
100999032982389.times do
name = Turboname::Domain.new(:from => dictionary)
name.save if name.length < 15 and name.available?
tld = name.tldize
name.save(tld) if tld and name.length < 15 and name.available?(tld)
end
turboname.rb is located in the same directory as go.rb. It's the same level. I just want to include this file in this script. I don't want to deal with gems or bundles.
./turboname.rb:1:in `require': no such file to load -- turboname/version (LoadError)
from ./turboname.rb:1
from go.rb:1:in `require'
from go.rb:1
Use a require_relative Statement
Recent Ruby versions no longer add . to the load path stored in $:. However, one solution is to use Kernel#require_relative to require a file relative to the current value of __FILE__. For example:
require_relative './turboname'
Note that this doesn't work in interactive REPL sessions with irb or pry, but works fine within actual source files.
The error isn't telling you it can't find ./turboname.rb. It's telling you that it found that file, but the first line of ./turboname.rb tries to require 'turboname/version', which Ruby can't find. Does ./turboname/version.rb exist? If so, is it readable by the current user?
If everything else checks out, then you have a load-path problem. At the top of go.rb, explicitly add the current working directory (or whichever directory contains turboname.rb and turboname/version.rb (possibly ./lib/) to your load path:
$LOAD_PATH << File.dirname(__FILE__) # for ./
# or
$LOAD_PATH << File.join(File.dirname(__FILE__), 'lib') # for ./lib/
With Ruby 2.0:
require "#{__dir__}/turboname"

require can't find an .rb file that's the same directory

How exactly does the require command in Ruby work? I tested it with the following two files that are in the same directory.
test.rb
require 'requirements'
square(2)
requirements.rb
def square(x)
x*x
end
But when I run ruby test.rb while I'm in the same directory as the files "test.rb" and "requirements.rb", I get the error:
/usr/local/rvm/rubies/ruby-1.9.3-p286/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- requirements (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p286/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from test.rb:1:in `<main>'
which I think means it can't find the requirements.rb file. But it's in the same directory as test.rb! How does one fix this?
Much thanks in advance. I apologize for such noob questions.
IIRC, ruby 1.9 doesn't include current dir ('.') to LOAD_PATH. You can do one of these:
# specify relative path
require './test1'
# use relative method
require_relative 'test1'
# add current dir to LOAD_PATH
$LOAD_PATH.unshift '.'
require 'test1'
I too just started to learn how ruby works, so I'm not perfectly sure if this helps. But try require_relative instead of require and I think it will work.
Afaik require searches in the ruby libary.

Resources