初次接触Ruby,在LOAD_PATH方面遇到困难[英] New to Ruby and am having trouble with LOAD_PATH

本文是小编为大家收集整理的关于初次接触Ruby,在LOAD_PATH方面遇到困难的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我最近通过从源代码编译OS X 10.8.3上安装了Ruby 2.0.0.我用

检查了版本
$ ruby --version
ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0]

我用

检查了我的宝石版本
$ 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

我注意到这里报告的Ruby的版本与我安装的版本不匹配.我不知道这是否重要.

我有一个简单的代码,称为engrypy.rb,我正在运行.使用GEM安装RSA安装" RSA"宝石后.

require 'rubygems'
require 'rsa'

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

当我运行它时,它会出现一个错误,说它找不到宝石.

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

所以问题是,为什么Ruby似乎不知道该宝石在哪里?我该如何修复?

推荐答案

Ruby 1.8.7用OS X发货.看来gem正在看到该版本,而不是手动安装的2.0.0.正如其他人已经建议的那样,使用RVM或RBENV(其中一个取决于个人喜好)使处理不同的红宝石环境变得更加容易.您可以在以下位置找到这些工具和描述:

rvm: https://rvm.io/

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

本文地址:https://www.itbaoku.cn/post/786345.html

问题描述

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