Disobedient 'gem env' Causing Rails "undefined method `gem'

I’m setting up on a textdrive shared host, and have installed my gems
to a local directory called ‘gems’. I did this by installing the gems
on my ubuntu 7.04 system and tar’ing up the ‘gems’ to a like-named
directory on the ‘gilford’ (FreeBSD 5.3) shared host. (a trick
required because ‘gem update’ or ‘gem install’ busts txd’s memory
limit during a zlib inflate of the gem repository hash - that is,
memory allocation fails for even the smallest of gems)

After setting my GEM_PATH and GEM_HOME so that ‘gem query’ reports
only my locally installed gems (as expected), and this test:

[xxx@gilford ~]$ gem list rubygems
*** LOCAL GEMS ***
rubygems-update (0.9.2)
RubyGems Update GEM

reports exactly what I expect, I next try ‘gem env’, which nonetheless
reports a back-rev gem version( ! ):

[xxx@gilford ~/local]$ gem env
Rubygems Environment:

  • VERSION: 0.8.11 (0.8.11)
  • INSTALLATION DIRECTORY: /users/home/xxx/gems
  • GEM PATH:
    • /users/home/xxx/gems
    • /users/home/xxx/local/lib/ruby/site_ruby/1.8
  • REMOTE SOURCES:

(note that I have removed the ‘gilford’ system’s gem path altogether,
in desperation. I’m trying to avoid all of txd’s gems, so that I am
certain that only my local stuff is included)

So I download the rubygems-0.9.2.tgz and stick it in a ‘local’
directory, and adjust the two GEM paths above, plus my normal shell
PATH and LD_LIBRARY_PATH to use the ‘local’ bins and libs first. But I
still get ‘gem env’ → 0.8.11

The actual ‘gem’ file in the ‘local/bin’ directory is simply some Ruby
that does:

require ‘rubygems’
Gem.manage_gems

with a few more lines. Surely these lines are run with either
GEM_PATH or the .gemrc path? And I think both of these are right:
‘gem query’ proves ‘GEM_PATH’ is working, and the next paragraph
shows .gemrc works as well.

(I also have a consistent .gemrc in my home dir. According to Jim
Weirich’s note from Apr 3 2006, I think I’m doing the right thing:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/187403

In short: “All gems use GEM_PATH, except for ‘gem’, which
uses .gemrc.” This behavior is supported by a test in which I add or
subtract ‘gilford’s’ gem path from my .gemrc: and as expected, my
‘gem env’ output shows only the lines specified in .gemrc.

The reason I care so much about what ‘gem env’ says is because when I
type “rails -v” I get:
/users/home/xxx/gems/bin/rails:15: undefined method `gem’ for
main:Object (NoMethodError)

which various posts say results from running current ‘rails’ with a
back-rev ‘gem’.
The shared host ‘gilford’ is running Ruby 1.8.4

‘gemwhich rubygems’ returns -> /usr/local/lib/ruby/site_ruby/1.8/
rubygems.rb BAD!

But my .gemrc explicitly specifies a gempath of /home/users/xxx/local/
lib/ruby/site_ruby/1.8 , and this directory does in fact have a
different version of ‘rubygems.rb’.

So why does ‘gemwhich rubygems’ find a file that is not on any path I
know of?
(‘require rubygems’ is supposed to employ the same lookup algorithm as
‘gemwhich’)

Can anyone explain what it is doing?

‘rubygems’ itself is found via a Ruby $LOAD_PATH lookup.

As the PickAxe book explains, this can be set by a ‘ruby -I xxx’
invocation.
But in the case of ‘gem’ itself, or of ‘rails’, there is no command
line ‘-I’ option.

So the library lookup path is specified by $RUBYLIB.
This is the Ruby analog to C’s $LD_LIBRARY_PATH

My .bash_profile now contains:

export RUBYLIB=$HOME/gems/local/lib/site_ruby/1.8
export GEM_PATH=$HOME/gems
export GEM_HOME=$HOME/gems

If I want some of txd’s stock (old) gems, I’d add their path as
well.kk
See http://rubygems.org/read/chapter/15