├── .gitignore ├── .rspec ├── Gemfile ├── ChangeLog.md ├── spec ├── spec_helper.rb └── ruby_version_spec.rb ├── .github └── workflows │ └── test.yml ├── Rakefile ├── ruby_version.gemspec ├── LICENSE.txt ├── README.md └── lib └── ruby_version.rb /.gitignore: -------------------------------------------------------------------------------- 1 | Gemfile.lock 2 | pkg 3 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --colour --format documentation 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | 5 | ### ### ### 6 | 7 | # 3.2 workaround for RSpec 2.99, see https://bugs.ruby-lang.org/issues/17391 8 | def File.exists?(f)exist?(f)end unless defined? File.exists? 9 | 10 | ### ### ### 11 | 12 | -------------------------------------------------------------------------------- /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## 1.0.3 / 2023-04-07 4 | 5 | * Remove old gem versions (`pkg` dir) from release 6 | 7 | ## 1.0.2 / 2020-01-04 8 | 9 | * Freeze string literals 10 | 11 | ## 1.0.1 / 2014-01-15 12 | 13 | * Fix VERSION constant 14 | * Add RubyVersion.revision method 15 | 16 | ## 1.0.0 / 2014-01-14 17 | 18 | * Moved from zucker 13.1 gem into its own gem 19 | 20 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'ruby_version' 2 | 3 | require 'rspec' 4 | require 'stringio' 5 | 6 | def capture_stdout 7 | capture = StringIO.new 8 | restore, $stdout = $stdout, capture 9 | yield 10 | $stdout = restore 11 | capture.string 12 | end 13 | 14 | def capture_stderr 15 | capture = StringIO.new 16 | restore, $stderr = $stderr, capture 17 | yield 18 | $stderr = restore 19 | capture.string 20 | end 21 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | name: Ruby ${{ matrix.ruby }} (${{ matrix.os }}) 8 | if: "!contains(github.event.head_commit.message, '[skip ci]')" 9 | strategy: 10 | matrix: 11 | ruby: 12 | - '3.2' 13 | - '3.1' 14 | - '3.0' 15 | - '2.7' 16 | - '2.6' 17 | - '2.5' 18 | - '2.4' 19 | - '2.3' 20 | - '2.2' 21 | - '2.1' 22 | - jruby 23 | - truffleruby 24 | os: 25 | - ubuntu-latest 26 | runs-on: ${{matrix.os}} 27 | steps: 28 | - uses: actions/checkout@v2 29 | - name: Set up Ruby 30 | uses: ruby/setup-ruby@v1 31 | with: 32 | ruby-version: ${{matrix.ruby}} 33 | bundler-cache: true 34 | - name: Run tests 35 | run: bundle exec rake 36 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # # # 2 | # Get gemspec info 3 | 4 | gemspec_file = Dir['*.gemspec'].first 5 | gemspec = eval File.read(gemspec_file), binding, gemspec_file 6 | info = "#{gemspec.name} | #{gemspec.version} | " \ 7 | "#{gemspec.runtime_dependencies.size} dependencies | " \ 8 | "#{gemspec.files.size} files" 9 | 10 | # # # 11 | # Gem build and install task 12 | 13 | desc info 14 | task :gem do 15 | puts info + "\n\n" 16 | print " "; sh "gem build #{gemspec_file}" 17 | FileUtils.mkdir_p 'pkg' 18 | FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg' 19 | puts; sh %{gem install --no-document pkg/#{gemspec.name}-#{gemspec.version}.gem} 20 | end 21 | 22 | # # # 23 | # Start an IRB session with the gem loaded 24 | 25 | desc "#{gemspec.name} | IRB" 26 | task :irb do 27 | sh "irb -I ./lib -r #{gemspec.name.gsub '-','/'}" 28 | end 29 | 30 | # # # 31 | # Spec 32 | 33 | desc "Run specs" 34 | task :spec do 35 | sh "rspec" 36 | end 37 | 38 | task :test => :spec 39 | task :default => :spec 40 | -------------------------------------------------------------------------------- /ruby_version.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | 3 | require File.expand_path('../lib/ruby_version', __FILE__) 4 | 5 | Gem::Specification.new do |gem| 6 | gem.name = "ruby_version" 7 | gem.version = RubyVersion::VERSION 8 | gem.summary = 'Adds the RubyVersion pseudo-constant.' 9 | gem.description = 'Provides a RubyVersion class which offers a convenient DSL for checking for the right Ruby version' 10 | gem.license = "MIT" 11 | gem.authors = ["Jan Lelis"] 12 | gem.email = ["hi@ruby.consulting"] 13 | gem.homepage = "https://github.com/janlelis/ruby_version" 14 | 15 | gem.files = Dir['{**/}{.*,*}'].select { |path| File.file?(path) && path !~ /^pkg/ } 16 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 17 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 18 | gem.require_paths = ['lib'] 19 | gem.metadata = { "rubygems_mfa_required" => "true" } 20 | 21 | gem.add_development_dependency 'rake', '>= 12.0' 22 | gem.add_development_dependency 'rspec', '~> 2.99' 23 | end 24 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Jan Lelis 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RubyVersion [Gem Version](https://badge.fury.io/rb/ruby_version) [](https://github.com/janlelis/ruby_version/actions?query=workflow%3ATest) 2 | 3 | Provides a `RubyVersion` to simplify checking for the right Ruby version in 4 | your programs. 5 | 6 | ## Setup 7 | 8 | On your command-line: 9 | 10 | $ gem install ruby_version 11 | 12 | In Ruby: 13 | 14 | require 'ruby_version' 15 | 16 | ## Usage 17 | 18 | # Output RUBY_VERSION 19 | RubyVersion.to_s 20 | 21 | # Check for the main version with a Float 22 | RubyVersion.is? 2.1 23 | 24 | # Use strings for exacter checking 25 | RubyVersion.is.above '1.9.2' 26 | RubyVersion.is.at_least '2.0.0' # or exactly, below, at_most 27 | 28 | # You can use the common comparison operators 29 | RubyVersion >= '1.8.7' 30 | RubyVersion.between? '1.8.7', '1.9.2' 31 | 32 | # Relase date checks 33 | RubyVersion.is.older_than Date.today 34 | RubyVersion.is.newer_than '2009-08-19' 35 | 36 | # Misc Accessors 37 | RubyVersion.major # => 1 38 | RubyVersion.minor # => 8 39 | RubyVersion.tiny # => 7 40 | RubyVersion.patchlevel # => 249 41 | RubyVersion.description # => "ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]" 42 | 43 | ## Also See 44 | 45 | - https://github.com/janlelis/ruby_engine 46 | - https://github.com/janlelis/ruby_info 47 | - https://github.com/rdp/os 48 | 49 | ## J-_-L 50 | 51 | Copyright (c) 2010-2014 Jan Lelis. MIT License. Originated from the zucker gem. 52 | -------------------------------------------------------------------------------- /spec/ruby_version_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe 'RubyVersion' do 4 | before :all do 5 | @remember_version = RUBY_VERSION 6 | capture_stderr{ RUBY_VERSION = '1.8.7' } 7 | end 8 | 9 | it 'should display RUBY_VERSION if called directly (to_s)' do 10 | RubyVersion.to_s.should == '1.8.7' 11 | end 12 | 13 | context 'with "is" method, with parameter' do 14 | it 'should check for main version (1.8 or 1.9) when Float paramater is given' do 15 | RubyVersion.is?( 1.8 ).should == true 16 | RubyVersion.is?( 1.9 ).should == false 17 | end 18 | 19 | it 'should check with string comparison if parameter is not Float' do 20 | RubyVersion.is?( '1.8' ).should == false 21 | end 22 | end 23 | 24 | context 'with "is" method, without parameter, but method chaining' do 25 | it 'should return a string for usage with comparison operators' do 26 | (RubyVersion.is > '1.8.7').should == false 27 | (RubyVersion <= '1.8.7').should == true 28 | (RubyVersion.is.between? '1.8.6', '1.8.7').should == true 29 | end 30 | 31 | it 'should create some handy compare aliases' do 32 | RubyVersion.is.above( '1.8.7' ).should == false 33 | RubyVersion.is.at_least( '1.8.7' ).should == true 34 | RubyVersion.is.exactly( '1.8.7' ).should == true 35 | end 36 | 37 | it 'also allows to check for the release dates' do 38 | RubyVersion.is.older_than( Date.today + 365 ).should == true 39 | RubyVersion.is.newer_than( '2000-01-01' ).should == true 40 | end 41 | end 42 | 43 | it 'should define some accessors' do 44 | RubyVersion.major.should == 1 45 | RubyVersion.minor.should == 8 46 | RubyVersion.tiny.should == 7 47 | RubyVersion.patchlevel.should == RUBY_PATCHLEVEL 48 | RubyVersion.description.should == RUBY_DESCRIPTION 49 | end 50 | 51 | after :all do 52 | capture_stderr{ RUBY_VERSION = @remember_version } 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/ruby_version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'date' 4 | require 'time' 5 | 6 | module RubyVersion 7 | VERSION = '1.0.3' 8 | 9 | class << self 10 | def to_s 11 | RUBY_VERSION 12 | end 13 | alias inspect to_s 14 | 15 | # comparable 16 | def <=>(other) 17 | value = case other 18 | when Integer 19 | RUBY_VERSION.to_i 20 | when Float 21 | RUBY_VERSION.to_f 22 | when String 23 | RUBY_VERSION 24 | when Date, Time 25 | other.class.parse(RUBY_RELEASE_DATE) 26 | else 27 | other = other.to_s 28 | RUBY_VERSION 29 | end 30 | value <=> other 31 | end 32 | include Comparable 33 | 34 | # chaining for dsl-like language 35 | def is?(other = nil) 36 | if other 37 | RubyVersion == other 38 | else 39 | RubyVersion 40 | end 41 | end 42 | alias is is? 43 | 44 | # aliases 45 | alias below < 46 | alias below? < 47 | alias at_most <= 48 | alias at_most? <= 49 | alias above > 50 | alias above? > 51 | alias at_least >= 52 | alias at_least? >= 53 | alias exactly == 54 | alias exactly? == 55 | def not(other) 56 | self != other 57 | end 58 | alias not? not 59 | alias between between? 60 | 61 | # compare dates 62 | def newer_than(other) 63 | if other.is_a? Date or other.is_a? Time 64 | RubyVersion > other 65 | else 66 | RUBY_RELEASE_DATE > other.to_s 67 | end 68 | end 69 | alias newer_than? newer_than 70 | 71 | def older_than(other) 72 | if other.is_a? Date or other.is_a? Time 73 | RubyVersion < other 74 | else 75 | RUBY_RELEASE_DATE < other.to_s 76 | end 77 | end 78 | alias older_than? older_than 79 | 80 | def released_today 81 | RubyVersion.date == Date.today 82 | end 83 | alias released_today? released_today 84 | 85 | # accessors 86 | 87 | def major 88 | RUBY_VERSION.to_i 89 | end 90 | alias main major 91 | 92 | def minor 93 | RUBY_VERSION.split('.')[1].to_i 94 | end 95 | alias mini minor 96 | 97 | def tiny 98 | RUBY_VERSION.split('.')[2].to_i 99 | end 100 | 101 | alias teeny tiny 102 | 103 | def patchlevel 104 | RUBY_PATCHLEVEL 105 | end 106 | 107 | def platform 108 | RUBY_PLATFORM 109 | end 110 | 111 | def release_date 112 | Date.parse RUBY_RELEASE_DATE 113 | end 114 | alias date release_date 115 | 116 | def description 117 | RUBY_DESCRIPTION 118 | end 119 | 120 | def revision 121 | defined?(RUBY_REVISION) && RUBY_REVISION 122 | end 123 | end 124 | end 125 | 126 | # J-_-L 127 | --------------------------------------------------------------------------------