Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the version of a gem from within Ruby?

Tags:

ruby

How can I get the version of a gem using just Ruby? I'd rather not use a system call and grep it the shells output like:

`gem search passenger`.scan(/(?:\(|, *)([^,)]*)/).flatten.first
=> "2.2.9"

Can't I just get it somehow with:

Gem::Version

I just don't know how to specify the gem I want, like in this case I want to get the Passenger gem's newest version.

like image 604
JP Silvashy Avatar asked Jan 13 '10 03:01

JP Silvashy


2 Answers

The rubygems' API is well documented.

The example you've been looking for:

>>> require 'rubygems'
>>> si = Gem::SourceIndex.from_installed_gems
>>> gems = si.find_name('rails')
>>> gems.each { |gem| puts gem.version.version }
2.3.5
like image 50
Damir Zekić Avatar answered Sep 30 '22 13:09

Damir Zekić


From "Determining which rubygem you're using":

Gemname::VERSION::STRING

also works.

like image 37
Andrew Grimm Avatar answered Sep 30 '22 13:09

Andrew Grimm