Determining which rubygem you're using

2019-01-26 11:46发布

问题:

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.

回答1:

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"


回答2:

puts Roo::VERSION::STRING



回答3:

gem list xxx it defaults to load the latest version.



回答4:

you can do use Gem::VERSION

require 'rubygems'; puts Gem::VERSION

that in a ruby program, which is quite useful if you want to make sure that your application under same version of rubygems accrossing all machines (e.g. bundler Gemfile)



回答5:

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.



标签: ruby rubygems