简体   繁体   中英

New to Ruby and am having trouble with LOAD_PATH

I recently installed Ruby 2.0.0 on OS X 10.8.3 by compiling from source code. I checked the version with

$ ruby --version
ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0]

And I checked my gem version with

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 2.0.3
  - RUBY VERSION: 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
  - INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
  - RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - universal-darwin-12
  - GEM PATHS:
     - /Library/Ruby/Gems/1.8
     - /Users/ehartsuyker/.gem/ruby/1.8
     - /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - :benchmark => false
     - :sources => ["http://rubygems.org/", "http://gemcutter.org"]
  - REMOTE SOURCES:
     - http://rubygems.org/
     - http://gemcutter.org

I noticed that the reported version of Ruby here does not match the version I installed. I don't know if this matters or not.

I have a simple piece of code called encrypy.rb that I'm running. After installing the 'rsa' gem with gem install rsa.

require 'rubygems'
require 'rsa'

key = RSA::KeyPair.generate(128)
ciphertext = key.encrypt("message")
puts(ciphertext)

When I run it, it gives an error saying it can't find the gem.

$ ruby encrypt.rb -Idirectory '/Library/Ruby/Gems/1.8'
/usr/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- rsa (LoadError)
    from /usr/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
    from encrypt.rb:2:in `<main>'

So the question is, why isn't Ruby finding the gem despite seeming to know where it is? And how can I fix it?

Ruby 1.8.7 is shipped with OS X. It seems that gem is seeing that version instead of your manually installed 2.0.0. As others already suggested, using RVM or rbenv (which one depends on personal preference) makes handling different ruby environments much easier. You can find those tools and descriptions for them in the following places:

RVM: https://rvm.io/

rbenv: https://github.com/sstephenson/rbenv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM