0

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:       ""
Folken
  • 447
  • 2
  • 7
  • 13
  • 1
    To specify relative path, you need `require_relative` since 1.9.2. Look at this question http://stackoverflow.com/questions/4005013/ruby-strange-error - is it what you ask? Or just try to add `require 'rubygems'` before your `require 'thegem'`. – Nakilon Dec 23 '10 at 07:07
  • thanks for the reply Nakilon - may I ask you though, if I download several libraries that have this problem, does this mean I have to go through them and either require_relative "/gem/some/path..." or do I have to add require "rubygems" to every source file that has this problem? Is there some sort of compatibility mode? – Folken Dec 23 '10 at 07:22
  • If `require 'rubygems'` solves your problem, I think it's normal to add this line into some top script of your program. – Nakilon Dec 23 '10 at 07:27
  • Ruby 1.9.2 automatically does a `require 'rubygems'`. 1.8.7 needs it though. – the Tin Man Dec 23 '10 at 09:00
  • What's the name of the gem in question? – the Tin Man Dec 23 '10 at 18:11

1 Answers1

3

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • I've added the otuput of those commands, it appears as if they are pointing to ~/.rvm. The 'require' is called from a file in the .rvm path: from :29:in `require' from /Users/username/.rvm/gems/ruby-1.9.2-head/gems/project/lib/path/class.rb:244:in `method' – Folken Dec 23 '10 at 14:20
  • @Folken, one thing I see is your rvm version is out of date. As of this morning we're on v.1.1.6, whereas you're on 1.0.1. Given how fast Wayne Seguin moves, that's a large number of changes. See if your rvm knows how to `rvm get head` to update itself. – the Tin Man Dec 23 '10 at 16:49
  • Here's what I think is happening. The gem you're using appears to have been written for Ruby 1.8+, not 1.9+. Ruby's search path changed in 1.9 which is why they added `require_relative`. Try switching rvm to use your system Ruby: `rvm use system` should do it, or use `rvm install 1.8.7; rvm use 1.8.7` and run your app. Your system Ruby probably won't have the gem you need so you might need to add it, so installing 1.8.7 and the needed gems into your ~/.rvm sandbox might be safer/better/wiser. – the Tin Man Dec 23 '10 at 18:10
  • It's correct for the current rvm. I think the old command was something like `rvm update --self`. – the Tin Man Dec 24 '10 at 03:00