With::Version

Gem Version Build Status

Syntax sugar for version checks. Inspired by usage in hashie.

Usage

Ruby::Version

Use with_minimum_ruby to check against a Ruby version at class load time, instead of runtime.

require 'with-version'

class Example
  include With::Version::Ruby

  with_minimum_ruby '2.4.0' do
    # only declared with Ruby 2.4.0 or newer
    def dig(*keys)
      puts "Digging #{keys.join(', ')} ..."
    end
  end
end
# Ruby 2.3.0
undefined method `dig' for #<Example:0x00007fca9388a8a0> (NoMethodError)
# Ruby 2.4.0
Digging x, y ...

It will automatically handle pre-release versions differently from when using Gem::Version, which is something developers often don't consider.

Gem::Version.new('2.4.0.pre') >= Gem::Version.new('2.4.0') # false
require 'with-version'

class Example
  include With::Version::Ruby

  with_minimum_ruby '2.4.0' do
    # also true if RUBY_VERSION = 2.4.0.pre
  end
end

Contributing

You're encouraged to contribute to this gem. See CONTRIBUTING for details.

Copyright (c) 2020, Daniel Doubrovkine and Contributors.

This project is licensed under the MIT License.