I have a gem installed but require 'gemname' does not work. Why?

前端 未结 11 1789
傲寒
傲寒 2020-12-14 00:02

The question I\'m really asking is why require does not take the name of the gem. Also, In the case that it doesn\'t, what\'s the easiest way to find the secret incantation

相关标签:
11条回答
  • 2020-12-14 00:14

    There is no standard for what the file you need to include is. However there are some commonly followed conventions that you can can follow try and make use of:

    • Often the file is called the same name as the gem. So require mygem will work.
    • Often the file is the only .rb file in the lib subdirectory of the gem, So if you can get the name of the gem (maybe you are itterating through vendor/gems in a pre 2.1 rails project), then you can inspect #{gemname}/lib for .rb files, and if there is only one, its a pretty good bet that is the one to require

    If all of that works, then all you can do is look into the gem's directory (which you can find by running gem environment | grep INSTALLATION | awk '{print $4}' and looking in the lib directory, You will probably need to read the files and hope there is a comment explaining what to do

    0 讨论(0)
  • 2020-12-14 00:14

    I too had this problem since installing OS X Lion, and found that even if I ran the following code I would still get the warning message. require 'rubygems' require 'nokogiri'

    I tried loads of solutions posted here and on the web, but in the end my work around solution was to simply follow the instructions at http://martinisoftware.com/2009/07/31/nokogiri-on-leopard.html to reinstall LibXML & LibXSLT from source, but ensuring the version of LibXML I installed matched the one that was expected by Nokogiri.

    Once I had done that, the warnings went away.

    0 讨论(0)
  • 2020-12-14 00:15

    Also rails people should remember to restart the rails server after installing a gem

    0 讨论(0)
  • 2020-12-14 00:16

    You need to include "rubygems" only if you installed the gem using gem . Otherwise , the secret incantation would be to fire up irb and try different combinations . Also , you can pass the -I option to the ruby interpreter so that you include the instalation directory of the gem , in the LOAD_PATH . Note that $LOAD_PATH is an array , which means you can add directories to it from within your script.

    0 讨论(0)
  • 2020-12-14 00:21

    The require has to map to a file in ruby's path. You can find out where gems are installed by running 'gem environment' (look for INSTALLATION DIRECTORY):

    kburton@hypothesisf:~$ gem environment
    RubyGems Environment:
      - RUBYGEMS VERSION: 1.2.0
      - RUBY VERSION: 1.8.7 (2008-08-08 patchlevel 71) [i686-linux]
      - INSTALLATION DIRECTORY: /usr/local/ruby/lib/ruby/gems/1.8
      - RUBY EXECUTABLE: /usr/local/ruby/bin/ruby
      - EXECUTABLE DIRECTORY: /usr/local/ruby/bin
      - RUBYGEMS PLATFORMS:
        - ruby
        - x86-linux
      - GEM PATHS:
         - /usr/local/ruby/lib/ruby/gems/1.8
      - GEM CONFIGURATION:
         - :update_sources => true
         - :verbose => true
         - :benchmark => false
         - :backtrace => false
         - :bulk_threshold => 1000
      - REMOTE SOURCES:
         - http://gems.rubyforge.org/
    kburton@editconf:~$ 
    

    You can then look for the particular .rb file you're attempting to require. Additionally, you can print the contents of $: from irb to see the list of paths that ruby will search for modules:

    kburton@hypothesis:~$ irb
    irb(main):001:0> $:
    => ["/usr/local/ruby/lib/ruby/site_ruby/1.8", "/usr/local/ruby/lib/ruby/site_ruby/1.8/i686-linux", "/usr/local/ruby/lib/ruby/site_ruby", "/usr/local/ruby/lib/ruby/vendor_ruby/1.8", "/usr/local/ruby/lib/ruby/vendor_ruby/1.8/i686-linux", "/usr/local/ruby/lib/ruby/vendor_ruby", "/usr/local/ruby/lib/ruby/1.8", "/usr/local/ruby/lib/ruby/1.8/i686-linux", "."]
    irb(main):002:0>
    
    0 讨论(0)
  • 2020-12-14 00:23

    It could also be the gem name mismatch:

    e.g. dummy-spi-0.1.1/lib/spi.rb should be named dummy-spi-0.1.1/lib/dummy-spi.rb

    then you can

    require 'dummy-spi'
    
    0 讨论(0)
提交回复
热议问题