How to Get the Version from a Gemspec File

How do I get the version from a gemspec file?

require "rubygems"

spec = Gem::Specification::load("example.gemspec")
puts spec.version

What is the built gemspec file (with version) for?

The .gem file is, well, the Gem. It contains the Gemspec (more precisely, a Marshal serialized version of the object graph of the Gemspec, rather than the executable .gemspec while which generates that object graph) as well as a compressed archive of all the files that are part of that gem (listed in the Gemspec). It also contains a cryptographically secure checksum and an optional cryptographic signature.

When you install a .gem, RubyGems checks the cryptographic checksum to make sure the Gem was not tampered with, checks the optional signature to make sure the Gem was created by who you think it was, reads the Gemspec to figure out what to do with the Gem, and unpacks it into the $GEM_HOME directory.

Kernel#require simply goes through the search path until it finds a file matching the name you provided and runs the file. It knows nothing about Gems. (It does know how to find the unpacked Gem directory, though.)

[Note: this is somewhat simplified. Kernel#require may have implementation-specific features, for example, YARV's Kernel#require also knows how to load dynamic libraries (e.g. .dlls, .sos, .dylibs, depending on the OS), JRuby's Kernel#require also knows how to load .jars, and so on.]

Using .gemspec version in documentation/library/script

There isn't a way, I don't think, because Rubygems is just an installation mechanism for Ruby libraries, which have no predefined notion of versioning.

However, possible workarounds could include using Gem.loaded_specs to find which gems have been loaded:

Gem.loaded_specs["mygemname"].version.to_s #=> "1.2.3"

Get ruby gem version without installation

You can see locked version in Gemfile.lock

cat Gemfile.lock | grep gem-name

Other option, but need bundle install first

bundle exec gem dependency | grep gem-name

Update:

If you need to check local gem version, for example some-gem.gem, you can use such command to parse all information from binary

gem specification some-gem.gem

or just

gem spec some-gem.gem

You can also look it with Ruby format

gem spec some-gem.gem --ruby

Of course you can use grep to filter lines with version word

But it's better to pass it as argument like this

gem spec some-gem.gem version

In a ruby .gemspec file, how do I specify multiple versions of a dependency?

Accordly to the documentation, if you want to have all version between 3 and 4, you can do this :

s.add_dependency "activeresource", ">= 3.0", "< 5.0"

The specifier accepted are : >=, ~>, <=, >, <.



Related Topics



Leave a reply



Submit