tags:

views:

3930

answers:

5

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 to require the damn thing!?

As an example if I have memcache-client installed then I have to require it using

require 'rubygems'
require 'memcache'
A: 

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.

Vhaerun
+4  A: 

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>
Kyle Burton
+7  A: 

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

Laurie Young
+1  A: 

The question I'm really asking is why require does not take the name of the gem.

Installing a gem gets the files onto your system. It doesn't make any claims as to what those files will be called.
As laurie points out there are several conventions for how they are named, but there's nothing to enforce that, and many gem authors unfortunately don't stick to them.

Also, In the case that it doesn't, what's the easiest way to find the secret incantation to require the damn thing!?

Read the docs for your gem?
I find googling for rdoc gemname will usually find the official rdocs for your gem, which usually show you how to use it.

Memcache is perhaps not the best example, as they assume you'll be using it from rails, and the 'require' will have already been done for you, but most other ones I've seen have examples which show the correct 'require' incantations

Orion Edwards
+8  A: 

My system also doesn't seem to know about RubyGems' existence - unless I tell it to. The 'require' command gets overwritten by RubyGems so it can load gems, but unless you have RubyGems already required it has no idea how to do that. So if you're writing your own, you can do:

require 'rubygems'
require 'gem-name-here'

If you're running someone else's code, you can do it on the command line with:

ruby -r rubygems script.rb

Also, there's an environment variable Ruby uses to determine what it should load up on startup:

export RUBYOPT=rubygems

(from http://www.rubygems.org/read/chapter/3. The environment variable thing was pointed out to me by Orion Edwards)

(If "require 'rubygems' doesn't work for you, however, this advice is of limited help :)

Atiaxi