2014-01-15 92 views
3

我想運行一個以前能正常工作的Ruby腳本。我需要使用機械化寶石,所以在腳本的頂部,它說require 'mechanize'。然而,當我嘗試現在運行腳本,終端輸出寫着:Ruby腳本無法找到所需的寶石

/Users/codebiker/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mechanize (LoadError) 
from /Users/codebiker/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' 
from myscript.rb:2:in `<main>' 

當我運行gem env,這裏建議,我得到:

RubyGems Environment: 
    - RUBYGEMS VERSION: 2.2.1 
    - RUBY VERSION: 2.1.0 (2013-12-25 patchlevel 0) [x86_64-darwin13.0] 
    - INSTALLATION DIRECTORY: /Users/codebiker/.rvm/gems/ruby-2.1.0 
    - RUBY EXECUTABLE: /Users/codebiker/.rvm/rubies/ruby-2.1.0/bin/ruby 
    - EXECUTABLE DIRECTORY: /Users/codebiker/.rvm/gems/ruby-2.1.0/bin 
    - SPEC CACHE DIRECTORY: /Users/codebiker/.gem/specs 
    - RUBYGEMS PLATFORMS: 
    - ruby 
    - x86_64-darwin-13 
    - GEM PATHS: 
    - /Users/codebiker/.rvm/gems/ruby-2.1.0 
    - /Users/codebiker/.rvm/gems/[email protected] 
    - GEM CONFIGURATION: 
    - :update_sources => true 
    - :verbose => true 
    - :backtrace => false 
    - :bulk_threshold => 1000 
    - :benchmark => false 
    - :sources => ["http://rubygems.org/", "http://gemcutter.org"] 
    - "install" => "--no-rdoc --no-ri" 
    - "update" => "--no-rdoc --no-ri" 
    - REMOTE SOURCES: 
    - http://rubygems.org/ 
    - http://gemcutter.org 
    - SHELL PATH: 
    - /Users/codebiker/.rvm/gems/ruby-2.1.0/bin 
    - /Users/codebiker/.rvm/gems/[email protected]/bin 
    - /Users/codebiker/.rvm/rubies/ruby-2.1.0/bin 
    - /usr/local/bin 
    - /usr/bin 
    - /bin 
    - /usr/sbin 
    - /sbin 
    - /usr/local/bin 
    - /opt/X11/bin 
    - /usr/texbin 
    - /Users/codebiker/.rvm/bin 

當我運行rvm get head,我得到:

Upgrading the RVM installation in /Users/codebiker/.rvm/ 
RVM PATH line found in /Users/codebiker/.bashrc /Users/codebiker/.zshrc. 
RVM sourcing line found in /Users/codebiker/.bash_profile /Users/codebiker/.zprofile. 
Upgrade of RVM in /Users/codebiker/.rvm/ is complete. 

當我運行which ruby,我得到:

/Users/codebiker/.rvm/rubies/ruby-2.1.0/bin/ruby

而且ruby -v給我:

ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin13.0]

任何意見,想法或建議,將不勝感激!我不知道這裏有什麼問題。

+1

可能太明顯了,寶石是否安裝正確? –

+0

輸入cmd'gem install mechanize' – fyz

回答

9

必須首先安裝Gems,然後纔可以在腳本中將require安裝到它們上面。安裝寶石;首先打開一個終端窗口,然後鍵入:

gem install GEM-NAME或在您的情況:gem install mechanize

+0

這完全解決了它 - 謝謝!但是,我不確定Mechanize爲什麼不在那裏。我之前在我的電腦上使用過機械化寶石,所以我只是假設沒有必要重新安裝它。爲什麼它會突然消失?這可能與我最近的Ruby 2.1安裝有關嗎? – CodeBiker

+3

是的,這是完全正確的。每個ruby安裝都會有一個獨立的寶石目錄。很高興我能幫上忙。快樂的編碼! – fyz

2

如果使用,添加到您的鏈接寶石:

gem 'mechanize' 

然後發出軟件包安裝您的或其他項目文件夾:

$ bundle install 

確保軟件包安裝後,寶石出現在中。

那麼即使在情況下,如果你不使用捆綁,確保捆綁創立創業板:

$ bundle show mechanize 
/path/to/mechanize/gem 

那麼如果你的應用程序是不是應用程序,只是運行你的應用程序

$ bundle exec ./your_app 
+0

非常感謝,謝謝!我投feed_me_code的答案是正確的,因爲這只是一個腳本,而不是一個完整的應用程序,但我很欣賞這個建議。我很好奇,如果你碰巧知道寶石爲什麼消失了。 (我永遠不會卸載它,因爲我使用它很多。) – CodeBiker

+1

@CodeBiker如果您使用rvm,則可以將其安裝到其他gemset中。或者如果您已經使用過根安裝,那麼也可以將其視爲其他gemset。 –