Require not able to find ruby file - ruby

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.

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.

ruby require_relative gives LoadError: cannot infer basepath inside IRB

I am currently in
Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day/units/
I can go into irb and require a file but it's a really long require...
require '/home/durrantm/Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day/units/login_as_admin_spec.rb'
=> true
I want to use require_relative, as in
$ cd /home/durrantm/Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day/
$ pwd
/home/durrantm/Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day
$ irb
irb(main):001:0> require_relative 'units/login_as_admin_spec.rb'
but I get:
LoadError: cannot infer basepath
require_relative requires a file relative to the file the call to require_relative is in. Your call to require_relative isn't in any file, it's in the interactive interpreter, therefore it doesn't work.
You can use the long form of require by explicitly passing the full path:
require './units/login_as_admin_spec.rb'
Or you add the current directory to the $LOAD_PATH and just require as usual:
$LOAD_PATH << '.'
require 'units/login_as_admin_spec'
This is a known bug in ruby:
Ruby bug #4487: require_relative fails in an eval'ed file
If you are using Pry, instead of IRB, this can be fixed by installing the pry-require_relative gem.
gem install pry-require_relative
This worked:
require File.expand_path("../login_as_admin_spec.rb", __FILE__)
require_relative works in the context of the current source file. This is different than the current working directory. I don't believe irb or pry have an understanding of "this current source file" concept; since you're not actually in a file.
In these REPLs, just use a relative path reference require './units/login_as_admin_spec.rb'.

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.

Rails irb default directory

I'm trying to include a source code file when I run irb but irb is unable to find it.
For example, say I am in the following directory in terminal:
/dan/rubyapp/
Assume I have a file named "firstapp.rb" in /dan/rubyapp/
I startup irb and from the irb prompt I type
> require "firstapp.rb"
but the file can't be found. If I type "Dir.pwd" it shows as
/dan/rubyapp/
The only way I can get "require" to work is if I include the full path like so
> require "/dan/rubyapp/firstapp.rb"
Is that the only way I can get this to work? All the tutorials I see online simply do "require file_name" so I assumed it would work.
here is the output from $: at irb
ruby-1.9.2-p0 > $:
=> ["/Users/Daniel/.rvm/gems/ruby-1.9.2-p0/gems/wirble-0.1.3/bin",
"/Users/Daniel/.rvm/gems/ruby-1.9.2-p0/gems/wirble-0.1.3/lib",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.4.0",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.4.0",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1",
"/Users/Daniel/.rvm/rubies/ruby-
1.9.2-p0/lib/ruby/1.9.1/x86_64-darwin10.4.0"]
The problem is that the current working directory is no longer in your path (as of Ruby 1.9.2). There are a few different ways around the problem.
1) In a ruby file itself, you can use the method require_relative instead of require. This will load a file relative to the loaction of the file containing the require_relative method:
http://extensions.rubyforge.org/rdoc/classes/Kernel.html
require_relative 'firstapp.rb'
This, however, will not work in irb.
2) Your other option is to include the current path in your argument to the require method. This will work in irb or in a ruby file. For instance:
require './firstapp.rb'
The reason this was implemented in ruby was to avoid inadvertently requiring the wrong file if there are different files with the same name in different directories in the path (similar to how *nix does not include the current directory "." in its path)
A couple of things to try:
1) Drop the .rb from the end of your require so you have:
require 'firstapp'
You don't normally add the .rb to a require (only to a load) - have a look here for more details:
http://www.fromjavatoruby.com/2008/10/require-vs-load.html
2) Failing that, make sure the current directory is on your load path - in irb execute:
p $:
and it will print out your ruby load path - check for an entry for "." (mine is the last entry)

Resources