tags:

views:

44

answers:

2

Hi.

I installed the new stable Ruby release and when I began to install gems I found out that the paths to them isn't added to the Ruby load path after successful installation of the gems.

What is the reason of this issue? How can I achieve it?

Thanks.

Here's my environment:

$ lsb_release -d
Description:    Debian GNU/Linux 5.0.6 (lenny)
$ cat ~/.gemrc
gem: --no-ri --no-rdoc
gemhome: /home/<username>/.gem
gempath:
  - /home/<username>/.gem
$ gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.7
  - RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [i686-linux]
  - INSTALLATION DIRECTORY: /home/<username>/.gem
  - RUBY EXECUTABLE: /usr/local/bin/ruby
  - EXECUTABLE DIRECTORY: /home/<username>/.gem/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
  - GEM PATHS:
    - /home/<username>/.gem
  - GEM CONFIGURATION:
    - :update_sources => true
    - :verbose => true
    - :benchmark => false
    - :backtrace => false
    - :bulk_threshold => 1000
    - "gem" => "--no-ri --no-rdoc"
    - "gemhome" => "/home/<username>/.gem"
    - "gempath" => ["/home/<username>/.gem"]
  - REMOTE SOURCES:
    - http://rubygems.org/
$ gem list
*** LOCAL GEMS ***

rack (1.2.1)
sqlite3-ruby (1.3.1)
$ ruby -e "puts $:"
# There's neither /home/<username>/.gem/gems/rack-1.2.1/lib
# nor home/<username>/.gem/gems/sqlite3-ruby-1.3.1/lib here.

/usr/local/lib/ruby/site_ruby/1.9.1
/usr/local/lib/ruby/site_ruby/1.9.1/i686-linux
/usr/local/lib/ruby/site_ruby
/usr/local/lib/ruby/vendor_ruby/1.9.1
/usr/local/lib/ruby/vendor_ruby/1.9.1/i686-linux
/usr/local/lib/ruby/vendor_ruby
/usr/local/lib/ruby/1.9.1
/usr/local/lib/ruby/1.9.1/i686-linux

Updated


I can't require any of the installed gems because they arn't in $:.

$ ruby -e "require 'rack'; puts $:"
:29:in `require': no such file to load -- rack (LoadError)
        from :29:in `require'
        from -e:1:in `'

But.

$ ruby -e "$: << '/home/<username>/.gem/gems/rack-1.2.1/lib'; require 'rack'; puts $:"
/usr/local/lib/ruby/site_ruby/1.9.1
/usr/local/lib/ruby/site_ruby/1.9.1/i686-linux
/usr/local/lib/ruby/site_ruby
/usr/local/lib/ruby/vendor_ruby/1.9.1
/usr/local/lib/ruby/vendor_ruby/1.9.1/i686-linux
/usr/local/lib/ruby/vendor_ruby
/usr/local/lib/ruby/1.9.1
/usr/local/lib/ruby/1.9.1/i686-linux
/home/<username>/.gem/gems/rack-1.2.1/lib # Here it is!

It works that way only : (

+1  A: 

You haven't loaded any gems yet:

ruby -e '
  puts "Before require: #{$:.grep /rack/}"
  require "rack"
  puts "After require: #{$:.grep /rack/}"
'
# Before require: []
# After require: ["C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/bin", 
#                 "C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib"]

ruby -e '
  puts "Before gem: #{$:.grep /rack/}"
  gem "rack"
  puts "After gem: #{$:.grep /rack/}"
  require "rack"
  puts "After require: #{$:.grep /rack/}"
'
# Before gem: []

# After gem: ["C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/bin", 
#             "C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib"]
# After require: ["C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/bin", 
#                 "C:/Ruby/YARV/1.9.2/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib"]
Jörg W Mittag
I can't require it because it isn't in $:. Look at the updates above.
Shamaoke
As you can clearly see from the code I posted, it isn't in my load path, either, and yet I *can* require it. Gems only get added to the load path *after* they have been required. (Or more precisely, after they have been *activated*, which `require` does automatically for you.) I have added a fourth and fifth example, which makes this even more obvious.
Jörg W Mittag
I don't know why but it works now. However for the `require` method only. The `load` method still raises **LoadError**. Don't know why Rubygems has so wierd behaviour. As for loading mechanism yes you was right it works exactly as you wrote. Thanks for the explanations.
Shamaoke
@Shamaoke: RubyGems overrides the `require` method so that when a file is not found in the load path, it looks through all the gems, and when it finds the file there, it activates the gem, adds it to the load path and retries the `require`. It does, however, not patch the `load` method.
Jörg W Mittag
So, the only way to `load` a gem is manually adding its directory to the load path and then actually load the file, isn't it?
Shamaoke
@Shamaoke: As you can see in my second example above, once you activate the gem, it is added to the load path. There is no need to do that manually.
Jörg W Mittag
A: 

things have changed for 1.9.2

http://wiki.github.com/rdp/ruby_tutorials_core/ruby-talk-faq#gem_loading_fails_191

it basically auto loads rubygems for you now, instead of pre-populating the load path.

rogerdpack
Thanks for clarification, **rogerdpack**. Now I understand the difference in the gem loading mechanismes between the current Rubygem version and the previous ones.
Shamaoke