require not working on ruby 2.0? - ruby

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.

Related

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

testing ruby code

I wrote regular expression in Ruby.
I created a folder name "test" and add 2 files in it.
1st file:
require 'test/unit'
class MyFirstTest < Test::Unit::TestCase
def test_for_truth
end
end
2nd file:
require 'my_math'
require 'test/unit'
class MyMathTest < Test::Unit::TestCcase
def test_addition
end
def test_subtraction
end
end
Now when i try to run this on ruby command line using command ruby my_math_test.rb.
it says
No such file or directory --ruby
I am using ruby 1.9.3
Do you know how to run this code?
Test unit tests can be run by entering:
rake test
See: http://guides.rubyonrails.org/testing.html
Not sure where is your my_math script. But I guess it is not in your $LOAD_PATH.
Try the following:
require_relative 'my_math'
I think, I found the answer
Add a complete path.
example:
require 'test/unit'
require 'c:/sites/cons/bets'
bets is the directory in which my .rb files are located.

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.

load error when run the ruby file from a different location

I have the following problem:
My ruby project structure : Ruby_Source\
file1.rb
file2.rb
file3.rb
In file1.rb,
require 'file2'
require 'file3'
now ,if I run the file1.rb from Ruby_Source, am not getting any error.
but , when I run the same from a different system location eg(c:)
error is Load error.
Can some one help me please?
You might want to use require_relative:
require_relative complements the builtin method require by allowing you to load a file that is relative to the file containing the require_relative statement.
See further discussion:
What is the difference between require_relative and require in Ruby?
And if you run Ruby 1.8:
Ruby: require vs require_relative - best practice to workaround running in both Ruby <1.9.2 and >=1.9.2
Try this:
require_relative 'file2'
in Ruby 1.9.x. It will search for file2 in the directory of file1.
In older versions you might try something like:
$: << File.dirname($0)
which will add the current program's path to the require-search path.

Ruby - LoadError on require

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.

Resources