tags:

views:

303

answers:

4

How can you determine which rubygem is being used in response to a "require" statement? gem which doesn't seem to help.

Background: for the project hornsby-herbarium-parser, I'm using the gem roo.

I used the github gem hmcgowan-roo , as at that time it was more recent than the rubyforge version of roo. I tried testing the code on runcoderun, and it failed because it doesn't have any version of roo. By this time, new versions of roo were available on both github and rubyforge.

I decided I may as well see if the latest version from rubyforge works for my code, as I assume rubyforge is more official, authoritative, and stable than github forks. Once I'm sure the rubyforge version works with my code, I'll ask runcoderun nicely if they can install it on their system.

I sudo gem installed roo, and my gems now include "hmcgowan-roo (1.3.5)" and "roo (1.3.6)", and running the tests for hornsby-herbarium-parser still pass. I know that as the rubyforge version was installed more recently, it ought to be the one being used in the tests, but I want to be able to verify this.

gem which roo

Didn't help, because it gave me

(checking gem hmcgowan-roo-1.3.5 for roo)
/usr/lib/ruby/gems/1.8/gems/hmcgowan-roo-1.3.5/lib/roo.rb

which I assume is the wrong answer.

Update: I used both

$:.detect {|dir| dir =~ /roo/}

and

puts Roo::VERSION::STRING

both agree with gem which, saying that I'm still using hmcgowan-roo-1.3.5.

A: 

The simplest way will be to uninstall and then reinstall the rubygems individually. I'm not so sure that your assumption about github not having stable sources is so accurate.

Cuervo's Laugh
+2  A: 

puts Roo::VERSION::STRING

gerrit
+1  A: 

First of all, rubygems will load the highest version number gem, not the most recently installed. So you should be getting roo 1.3.6 here.

Other than that, I second gerrit's suggestion to look for a version constant. For instance, I just loaded rmagick and there is a constant Magick::Version. Another example is Open4::VERSION.

However not all gems have a version constant, so as a fallback you could do something hacky like:

>> require 'open4'
=> true

>> $:.detect {|dir| dir =~ /\/open4-([^\/]*)\//}
=> "/Library/Ruby/Gems/1.8/gems/open4-0.9.6/bin"

>> $1
=> "0.9.6"
Daniel Lucraft
I didn't think of $: , thanks. However, with the regular expression, if someone created johndoe-open4, then it wouldn't match.
Andrew Grimm
True, true. This is why I called it 'hacky' :) But maybe you could fix it up a bit more.
Daniel Lucraft
A: 

gem list xxx it defaults to load the latest version.

rogerdpack