This gem has a very niche purpose, which is:

  1. providing introspection of a Version module based on a Version::VERSION constant string,
  2. while not interfering with gemspec parsing where the VERSION string is traditionally used.

If this isn't precisely your use case you may be better off looking at versionaire, a wonderful, performant, well-maintained, gem from the Alchemists, or version_sorter from GitHub.

For more discussion about this see issue #2

[![Liberapay Patrons][⛳liberapay-img]][⛳liberapay] [![Sponsor Me on Github][🖇sponsor-img]][🖇sponsor] Buy me coffee donation button Patreon donate button

Still here?

Give your next library an introspectable Version module without breaking your Gemspec.

MyLib::Version.to_s # => "1.2.3.rc3"
MyLib::Version.major # => 1
MyLib::Version.minor # => 2
MyLib::Version.patch # => 3
MyLib::Version.pre # => "rc3"
MyLib::Version.to_a # => [1, 2, 3, "rc3"]
MyLib::Version.to_h # => { major: 1, minor: 2, patch: 3, pre: "rc3" }

This library was extracted from the gem oauth2.

This gem has no runtime dependencies.

Project bundle add version_gem
1️⃣ name, license, docs, standards License: MIT SemVer 2.0.0 Keep-A-Changelog 1.0.0
2️⃣ version & activity Gem Version Total Downloads Download Rank Source Code Open PRs Closed PRs
3️⃣ maintenance & linting Maintainability Helpers Depfu Contributors Style Kloc Roll
4️⃣ testing Supported Heads Unofficial Support <!--MacOS Windows-->
5️⃣ coverage & security CodeClimate CodeCov Coveralls Security Policy CodeQL Code Coverage
6️⃣ resources Get help on Codementor Chat Blog Wiki
7️⃣ spread 💖 Liberapay Patrons Sponsor Me Tweet @ Peter 🌏 👼 💻


Install the gem and add to the application's Gemfile by executing:

$ bundle add version_gem

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install version_gem


In the standard bundle gem my_lib code you get the following in lib/my_lib/version.rb:

module MyLib
  VERSION = "0.1.0"

Change it to a nested Version namespace (the one implied by the path => namespace convention):

module MyLib
  module Version
    VERSION = "0.1.0"

Now add the following near the top of the file the manages requiring external libraries. Using the same example of bundle gem my_lib, this would be lib/my_lib.rb.

require "version_gem"

Then, add the following wherever you want in the same file (recommend the bottom).

MyLib::Version.class_eval do
  extend VersionGem::Basic

And now you have some version introspection methods available:

MyLib::Version.to_s # => "0.1.0"
MyLib::Version.major # => 0
MyLib::Version.minor # => 1
MyLib::Version.patch # => 0
MyLib::Version.pre # => ""
MyLib::Version.to_a # => [0, 1, 0]
MyLib::Version.to_h # => { major: 0, minor: 1, patch: 0, pre: "" }

Side benefit

Your version.rb file now abides the Ruby convention of directory / path matching the namespace / class!


The pattern of version.rb breaking the ruby convention of directory / path matching the namespace / class is so entrenched that the zeitwerk library has a special carve-out for it. RubyGems using this "bad is actually good" pattern are encouraged to use Zeitwerk.for_gem.

Do not do that ^ if you use this gem.

Simple Zeitwerk Example

Create a gem like this (keeping with the MyLib theme):

bundle gem my_lib

Then following the usage instructions above, you edit your primary namespace file @ lib/my_lib.rb, but inject the Zeitwerk loader.

# frozen_string_literal: true

require_relative "my_lib/version"

module MyLib
  class Error < StandardError; end
  # Your code goes here...

loader =
loader.tag = File.basename(__FILE__, ".rb")
loader.push_dir("lib/my_lib", namespace: MyLib)
loader.setup # ready!
loader.eager_load(force: true) # optional!

MyLib::Version.class_eval do
  extend VersionGem::Basic

Complex Zeitwerk Example

Query Ruby Version (as of version 1.2.0)

In Continuous Integration environments for libraries that run against many versions of Ruby, I often need to configure things discretely per Ruby version, and doing so forced me to repeat a significant amount of boilerplate code across each project.

Thus VersionGem::Ruby was born. It has the two optimized methods I always need:

engine = "ruby"
version = "2.7.7"
gte_minimum_version?(version, engine)  # Is the current version of Ruby greater than or equal to some minimum?

major = 3
minor = 2
actual_minor_version?(major, minor, engine) # Is the current version of Ruby precisely a specific minor version of Ruby?

Version::Ruby is not loaded by default. If you want to use it, you must require it as:

require "version_gem/ruby"

Normally I do this in my spec/spec_helper.rb, and/or .simplecov files. Occasionally in my Rakefile.


This design keeps your version.rb file compatible with the way gemspec files use them. This means that the introspection is not available within the gemspec. The enhancement from this gem is only available at runtime.

RSpec Matchers

In spec_helper.rb:

require "version_gem/rspec"

Then you can write a test like:

RSpec.describe(MyLib::Version) do
  it_behaves_like "a Version module", described_class

# Or, if you want to write your own, here is the a la carte menu:
RSpec.describe(MyLib::Version) do
  it "is a Version module" do
    expect(described_class.to_h.keys).to(match_array(%i[major minor patch pre]))


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to





Made with contributors-img.


The gem is available as open source under the terms of the MIT License License: MIT. See LICENSE for the official Copyright Notice.

Code of Conduct

Everyone interacting in the VersionGem project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.


This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.

As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.

For example:

spec.add_dependency("version_gem", "~> 1.1")