I have the following two files: main.rb and sort.rb located in the same folder. In main.rb I have the following code:
require 'sort'
Sort.insertion_sort([1,2,3,4]).each {|x| print "#{x}, "}
When I try and run this via ruby main.rb I get the following error:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- sort (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from main.rb:1:in `<main>'
Any ideas why?
Thanks
The better way to use
require_relative "sort"
intead of
require "sort"
Thanks, #Jörg W Mittag.
Or you can add a path where ruby should search your files (can be a security risk):
$:.unshift File.join(File.dirname(__FILE__), ".") # current directory
require 'sort'
try require 'sort.rb' and check permissions
you would also:
require directory/sort.rb
In Ruby 1.9.2, $: doesn't include the current directory ('.'). Either do relative_require instead, or do $: << '.'.
Joerg Mittag says that $: << '.' shouldn't be done because it's a security risk.
Related
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.
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.
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"
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.
This question already has answers here:
Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?
(7 answers)
Closed 8 years ago.
Is there any reason why my present working directory is not on my Ruby path?
Consider:
~:499$ irb
ruby-1.9.2-p136 :002 > puts $:
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1
/Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/x86_64-darwin10.6.0
=> nil
This is really bothering me because require isn't working as I thought it would (although I'm a ruby nuby):
require 'some_file_that_I_know_darn_well_is_in_pwd.rb'
If I append '.' to the end, then the require works as I'd expect.
What am I missing?
UPDATE:
Arg! Now I'm getting a new problem. Consider:
ruby-1.9.2-p136 :010 > `ls`
=> "start.rb\n"
ruby-1.9.2-p136 :011 > require_relative 'start'
LoadError: cannot infer basepath
from (irb):11:in `require_relative'
from (irb):11
from /Users/mrberryman/.rvm/rubies/ruby-1.9.2-p136/bin/irb:16:in `<main>'
Now what's up?
In Ruby 1.9.2 the Powers that Be introduced an explicit change so that the working directory is no longer in the Ruby path. I thought it was the Apocalypse and a terrible thing, until I learned about require_relative. My apps tend to look like this:
require 'some_gem'
require 'another_gem'
require_relative 'lib/init'
And then lib/init.rb can have:
require_relative 'lib1' # this is lib/lib1.rb
require_relative 'lib2' # this is lib/lib2.rb
It's the bees knees, and solves all sorts of problems I used to have with requiring the same file from different working directories.
Edit: Unfortunately (for reasons I don't know and haven't looked into) require_relative doesn't work specifically in irb. For this you can:
do what you initially described: either $: << '.' or $:.unshift '.', or
you can use load 'myfile.rb' or require './myfile' instead:
irb(main):001:0> Dir['*.rb']
=> ["a.rb", "bar.rb", "foo.rb", "prime.rb", "tmp.rb"]
irb(main):002:0> require 'a'
LoadError: no such file to load -- a
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from (irb):2
from /usr/local/bin/irb:12:in `<main>'
irb(main):003:0> require_relative 'a'
LoadError: cannot infer basepath
from (irb):3:in `require_relative'
from (irb):3
from /usr/local/bin/irb:12:in `<main>'
irb(main):004:0> load 'a.rb'
a
=> true
irb(main):005:0> require './a'
a
=> true
You can use require_relative assuming it does what you need.
Make sure that the environment variable "RUBYLIB" is set with all directory paths where you will find custom *.rb code. It drove me nuts too.