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

前端 未结 11 1820
傲寒
傲寒 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: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>
    

提交回复
热议问题