19

Is there a clean way to extract the version string from a .gemspec file? (The gem is not yet installed)

e.g. somethingcool.gemspec is

Gem::Specification.new do |s|
  s.name = "somethingcool"
  s.version = "1.2.3"
  ... etc ...
end

I want to extract "1.2.3".

I could grep it out but there must be a better way.

Gavin Brock
  • 5,027
  • 1
  • 30
  • 33

1 Answers1

36
require "rubygems"

spec = Gem::Specification::load("example.gemspec")
puts spec.version
duncan
  • 6,113
  • 3
  • 29
  • 24
  • There is also https://github.com/packsaddle/ruby-parse_gemspec gem, [implemented](https://github.com/packsaddle/ruby-parse_gemspec/blob/master/lib/parse_gemspec/specification.rb) as a thin wrapper over `Gem::Specification.load`, and [a CLI](https://github.com/packsaddle/ruby-parse_gemspec-cli) printing out JSON. – Beni Cherniavsky-Paskin Feb 25 '18 at 11:21