A problem with the “require” keyword not finding t

2019-08-23 12:11发布

问题:

I have a problem with the "require" keyword in ruby, I just downloaded a gem using:

gem install thegem

And when I tried running an example I received the following error:

<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- 
    ./thegem/some/path (LoadError)

In the code where the problem is:

needs_to_load = a_class
require "gem/some/path/#{needs_to_load}"

I'm a Ruby noob learning my way around and am not sure what this is, a google search didn't help but revealed that it may be due to needing a relative path, however changing the 2nd line to the following did not help:

require "./gem/some/path/#{needs_to_load}"

I'm using RVM with the following ruby version: ruby-1.9.2-head.

(p.s. sorry if this has been asked before I didn't see anything for it in the related questions field)

[Edit: appending the output of 'rvm info' and 'gem env' per Tin Man's request below]

$ gem env

RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.7
  - RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [i386-darwin9.8.0]
  - INSTALLATION DIRECTORY: /Users/username/.rvm/gems/ruby-1.9.2-head
  - RUBY EXECUTABLE: /Users/username/.rvm/rubies/ruby-1.9.2-head/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/username/.rvm/gems/ruby-1.9.2-head/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-darwin-9
  - GEM PATHS:
     - /Users/username/.rvm/gems/ruby-1.9.2-head
     - /Users/username/.rvm/gems/ruby-1.9.2-head@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

$ rvm info

ruby-1.9.2-head:

  system:
    uname:       "Darwin macintosh 10.5.0 Darwin Kernel Version 10.5.0: Fri Nov  5 23:20:39 PDT 2010; root:xnu-1504.9.17~1/RELEASE_I386 i386"
    zsh:         "zsh 4.3.9 (i386-apple-darwin10.0)"
    bash:        "GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)"

  rvm:
    version:      "rvm 1.0.1 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]"

  ruby:
    interpreter:  "ruby"
    version:      "1.9.2p0"
    date:         "2010-08-18"
    platform:     "i386-darwin9.8.0"
    patchlevel:   "2010-08-18 revision 29034"
    full_version: "ruby 1.9.2p0 (2010-08-18 revision 29034) [i386-darwin9.8.0]"

  homes:
    gem:          "/Users/username/.rvm/gems/ruby-1.9.2-head"
    ruby:         "/Users/username/.rvm/rubies/ruby-1.9.2-head"

  binaries:
    ruby:         "/Users/username/.rvm/rubies/ruby-1.9.2-head/bin/ruby"
    irb:          "/Users/username/.rvm/rubies/ruby-1.9.2-head/bin/irb"
    gem:          "/Users/username/.rvm/rubies/ruby-1.9.2-head/bin/gem"
    rake:         "/Users/username/.rvm/gems/ruby-1.9.2-head/bin/rake"

  environment:
    PATH:         "/Users/username/.rvm/gems/ruby-1.9.2-head/bin:/Users/username/.rvm/gems/ruby-1.9.2-head@global/bin:/Users/username/.rvm/rubies/ruby-1.9.2-head/bin:/Users/username/.rvm/bin:/Library/Frameworks/Python.framework/Versions/2.6/bin:/sw/bin:/sw/sbin:/opt/local/bin:/opt/local/sbin:/opt/git/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/mysql/bin:/usr/X11R6/bin"
    GEM_HOME:     "/Users/username/.rvm/gems/ruby-1.9.2-head"
    GEM_PATH:     "/Users/username/.rvm/gems/ruby-1.9.2-head:/Users/username/.rvm/gems/ruby-1.9.2-head@global"
    BUNDLE_PATH:  "/Users/username/.rvm/gems/ruby-1.9.2-head"
    MY_RUBY_HOME: "/Users/username/.rvm/rubies/ruby-1.9.2-head"
    IRBRC:        "/Users/username/.rvm/rubies/ruby-1.9.2-head/.irbrc"
    RUBYOPT:      ""
    gemset:       ""

回答1:

If you use a normal gem install somegem, then you should be able to require 'somegem', without any path added to the name of the gem. require_relative is useful for gems and modules that are installed somewhere out of the normal Ruby search paths, such as ones you've written and embedded in a lib directory immediately below the calling script's directory.

Use gem env and append its output to your original question by editing it, which will help us determine if it's a pathing issue. Because you are using RVM, your gem path should be pointing into your ~/.rvm folder. If you do rvm info it will also give you path info, so compare the output of those two commands to see if they are consistent.



标签: ruby gem require