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. .dll
s, .so
s, .dylib
s, depending on the OS), JRuby's Kernel#require
also knows how to load .jar
s, 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
Star Rating in Ajax with Ruby on Rails
Why Is Devise Not Displaying Authentication Errors on Sign in Page
Aptana 3 Ruby Debugger - Exception in Debugthread Loop: Undefined Method 'Is_Binary_Data'
How to Add Child Nodes in Nodeset Using Nokogiri
How Does MACports Install Packages? How to Activate a Ruby Installation Done via MACports
Using Soap and Other Standard Libraries in Ruby 1.9.2
Can a Ruby Script Tell What Directory It's In
How to Add Confirm Message with Link_To Ruby on Rails
Why Do I Get an "Undefined Method for 'Has_Attached_File' When Installing Paperclip
I'm Getting "Found Character That Cannot Start Any Token While Scanning for the Next Token"
Ruby - Activerecord::Connectionnotestablished
Named Parameters in Ruby Structs
Why Is Date.Today - 6.Months + 6.Months != Date.Today