Do ruby and irb use different module search paths? - ruby

I have a Ruby script that is trying to require the restclient module. When I reduce it down to just this one line, it still fails:
#!/usr/bin/env ruby
require 'restclient'
When I run it, I get the following error:
./test.rb:3:in `require': no such file to load -- restclient (LoadError)
from ./test2.rb:3
When I run irb, the module loads fine:
$ irb
>> require "restclient"
=> true
>>
As far as I can tell, it looks like both the script and irb have the same module paths:
$ ruby -e "puts $:"
/Library/Ruby/Site/1.8
/Library/Ruby/Site/1.8/powerpc-darwin10.0
/Library/Ruby/Site/1.8/universal-darwin10.0
/Library/Ruby/Site
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8/universal-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/powerpc-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0
.
$ irb
>> puts $:
/Library/Ruby/Site/1.8
/Library/Ruby/Site/1.8/powerpc-darwin10.0
/Library/Ruby/Site/1.8/universal-darwin10.0
/Library/Ruby/Site
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8/universal-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/powerpc-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0
.
=> nil
>>
What would cause a module to load through irb, but not when run directly through Ruby?
One other confusing detail is that the restclient gem doesn't seem to be in my path to start with. How is irb finding it?
$ locate restclient | grep gems
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/bin/restclient
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/abstract_response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/exceptions.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/net_http_ext.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/payload.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/raw_response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/request.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/resource.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/spec/restclient_spec.rb
Thanks - Marc

Try
require "rubygems"
in the source code file, or starting the ruby program with ruby -rubygems filename.rb.

Related

require not working on ruby 2.0?

I'm doing a test with "require" under ruby 2.0.0p576 (2014-09-19 revision 47628) [x86_64-darwin13.4.0] it doesn't work in many ways.
There are two files in ruby directory as shown below:
string_extensions.rb
class String
def vowels
self.scan(/[aeiou]/i)
end
end
vowels_test.rb
require 'string_extensions'
puts "This is a test".vowels.join('-')
fire up IRB
Snailwalkers-MacBook-Pro:ruby snailwalker$ ruby vowels_test.rb
returs : `require': cannot load such file -- string_extensions (LoadError)
I tried to change require 'string_extensions' to " require_relative 'string_extensions' ; require './string_extensions.rb' . They all didn't work.
both return error : vowels_test.rb:1:in require_relative': /Users/snailwalker/Ruby/string_extensions.rb:1: class/module name must be CONSTANT (SyntaxError)
Your help will be greatly appreciated!
You can use require_relative instead:
require_relative 'string_extensions'
puts "This is a test".vowels.join('-')
Or even require './string_extensions'.
Use:
ruby -I. vowels_test.rb
Automatic inclusion of the current directory in the load paths was removed in Ruby 2.

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.

Console not ready ruby sublime text file

I'm new to programming. Just about to start learning Ruby. I already took a console class, but I am stuck here.
I'm using a mac 10.6.8. I have done a quick 1+2 in the sublime text editor. I saved it. I went over to my console typed irb and then typed ruby example.rb. I have read elsewhere here that typing require './example' would help....it didn't. I am getting the following
NameError: undefined local variable or method `example' for main:Object
from (irb):2
from /usr/local/rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
I don't understand what I am doing wrong. Thank you for your help. I really appreciate it.
-L
I would do as below:
kirti#kirti-Aspire-5733Z:~$ irb
2.0.0p0 :001 > require 'fileutils'
=> true
2.0.0p0 :002 > FileUtils.pwd
=> "/home/kirti"
2.0.0p0 :003 > FileUtils.cd "/home/kirti/ruby"
=> nil
2.0.0p0 :004 > load "./SO.rb"
3
=> true
2.0.0p0 :005 > require "./SO.rb"
3
=> true
My SO.rb file contains the below line :
puts 1+2
May be you wanna give a try.
Step 1: Navigate to your project/file folder by using command "cd folder_name/folder_location"
Step 2: load './example.rb'
For better solution you may wanna define some function inside example.rb
Like:
def sum
1 + 2
end
And to get the output enter sum in irb after loading the example.rb file.
irb is the interactive ruby shell. Within the shell, everything you type is interpreted as Ruby code, not bash commands. So, for example:
bash> puts 1 + 2
# command not found: puts
# this happens because you're not in a Ruby shell
bash> irb
# now you're in a Ruby shell
irb> puts 1 + 2
# 3
If you wrote some code in example.rb, you have two options:
From the bash shell, run ruby example.rb (from the same directory where your example.rb file is saved.
From the irb console, you can require 'example', which will load the contents of example.rb into your interpreter. In this case, it will immediately execute the Ruby code. If you wrapped the contents of example.rb in a class, it would load the class, but not execute code within it until you instantiated/called it.
Hopefully that helps!
My guess is that you are typing (into irb):
require example.rb
When you need to type:
require './example.rb'
The first tells ruby: "require what is in a variable called example". Because you did not define a variable called example, it results in the no variable or method error.
The second tells ruby: "require a string './example.rb'". Since the require method essentially knows how to find the file name passed as a string and evaluate the file, you'll get the right output
By the way, for this example, example.rb needs to be in the same directory. If example.rb is in another directory, you'll need to use the full path (I won't expand on it here) to source it.
You'll also notice that the output will look something like this:
3
=> true
This is because the file was evaluated (executing the code: puts 1+2) and the require method returns true to indicate it evaluated the file.
If you require the file again, you'll get false because the file is already loaded.

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

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