Difficulty loading local class files with "ruby1.9.1" on Ubuntu 11.10

I’m running 11.10 Oneric, but am having a problem upgrading from default
Ruby 1.8 to recommended 1.9.

Here’s a local file, Test2.rb:

class Test2
puts “hello, from Test2!”
end

And here’s Test.rb:

require ‘Test2’
puts RUBY_VERSION

The important line is the first one: the require.

I can run from 1.8 fine:

% ruby1.8 < Test.rb
hello, from Test2!
1.8.7

But from 1.9.3…

% ruby1.9.1 < Test.rb
internal:lib/rubygems/custom_require:29:in require': no such file to load -- Test2 (LoadError) from <internal:lib/rubygems/custom_require>:29:inrequire’
from -:1:in `’

Non-local requires are fine:

% ruby1.9.1 << EOF
require ‘rubygems’
EOF
% ruby1.8 << EOF
require ‘rubygems’
EOF

Same problem running irb, where here I’m manually switching between Ruby
versions, and it works with 1.8 but not 1.9:

% irb
irb(main):001:0> puts RUBY_VERSION
1.8.7
=> nil
irb(main):002:0> require ‘Test2’
hello, from Test2!
=> true
irb(main):003:0> exit
% sudo update-alternatives --config ruby
[sudo] password for djconnel:
There are 2 choices for the alternative ruby (providing /usr/bin/ruby).

Selection Path Priority Status

  • 0 /usr/bin/ruby1.8 50 auto mode
    1 /usr/bin/ruby1.8 50 manual mode
    2 /usr/bin/ruby1.9.1 10 manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/ruby1.9.1 to provide /usr/bin/ruby
(ruby) in manual mode.
% irb
irb(main):001:0> puts RUBY_VERSION
1.9.2
=> nil
irb(main):002:0> require ‘Test2’
LoadError: no such file to load – Test2
from internal:lib/rubygems/custom_require:29:in require' from <internal:lib/rubygems/custom_require>:29:inrequire’
from (irb):2
from /usr/bin/irb:12:in `’
irb(main):003:0> exit
% sudo update-alternatives --config ruby
There are 2 choices for the alternative ruby (providing /usr/bin/ruby).

Selection Path Priority Status

0 /usr/bin/ruby1.8 50 auto mode
1 /usr/bin/ruby1.8 50 manual mode

  • 2 /usr/bin/ruby1.9.1 10 manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/ruby1.8 to provide /usr/bin/ruby
(ruby) in manual mode.
% irb
irb(main):001:0> puts RUBY_VERSION
1.8.7
=> nil
irb(main):002:0> require ‘Test2’
hello, from Test2!
=> true

Any help?

thanks!
Dan

use “Kernel#require_relative”, later Ruby 1.9.2

require ‘./test2.rb’

worked for my

but without the

./

did not !!

webber han wrote in post #1048382:

Must be the ruby $LOAD_PATH in 1.8 include the current working
directory,
but with 1.9, it does not. How about print out the $LOAD_PATH in two
ruby versions and see what’s the difference?

Thanks!!! That’s it…

under 1.9.1:
irb(main):002:0> puts $LOAD_PATH
/usr/local/lib/site_ruby/1.9.1
/usr/local/lib/site_ruby/1.9.1/x86_64-linux
/usr/local/lib/site_ruby
/usr/lib/ruby/vendor_ruby/1.9.1
/usr/lib/ruby/vendor_ruby/1.9.1/x86_64-linux
/usr/lib/ruby/vendor_ruby
/usr/lib/ruby/1.9.1
/usr/lib/ruby/1.9.1/x86_64-linux
=> nil

under 1.8:

irb(main):001:0> puts $LOAD_PATH
/usr/local/lib/site_ruby/1.8
/usr/local/lib/site_ruby/1.8/x86_64-linux
/usr/local/lib/site_ruby
/usr/lib/ruby/vendor_ruby/1.8
/usr/lib/ruby/vendor_ruby/1.8/x86_64-linux
/usr/lib/ruby/vendor_ruby
/usr/lib/ruby/1.8
/usr/lib/ruby/1.8/x86_64-linux
.
=> nil

So my conclusion is never assume the current directory is in the load
path…

I change the script to :
% cat Test.rb
$LOAD_PATH << “.”
require ‘Test2’
puts RUBY_VERSION

and now it runs in both versions:
% ruby1.8 Test.rb
hello, from Test2!
1.8.7
% ruby1.9.1 Test.rb
hello, from Test2!
1.9.2

BTW, changing to
require ‘./Test2’
also works: thanks for that!

gabe gabriellini wrote in post #1048372:

require ‘./test2.rb’

worked for my

but without the

./

did not !!

Must be the ruby $LOAD_PATH in 1.8 include the current working
directory,
but with 1.9, it does not. How about print out the $LOAD_PATH in two
ruby versions and see what’s the difference?