|
| 1 | +## |
| 2 | +# Methods to work with the current ruby version at the MAJOR.MINOR level |
| 3 | +# |
| 4 | +module RubyVersion |
| 5 | + class << self |
| 6 | + |
| 7 | + # Returns the latest known version of Ruby |
| 8 | + def latest_version |
| 9 | + Gem::Version.new('3.4') |
| 10 | + end |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + # Returns true if the current verion of Ruby is as defined |
| 15 | + def latest? |
| 16 | + is?(latest_version) |
| 17 | + end |
| 18 | + |
| 19 | + |
| 20 | + # Returns tru if the current version of Ruby matches the expected version |
| 21 | + # |
| 22 | + # expected: anything that can be stringified with String(xxx) |
| 23 | + # |
| 24 | + def is?(expected = nil) |
| 25 | + expected = refined(expected) |
| 26 | + expected == current |
| 27 | + end |
| 28 | + |
| 29 | + |
| 30 | + # Return a Gem::Version of the current Ruby version twith only MJOR.MINOR segments |
| 31 | + def current |
| 32 | + @current_version ||= refined RUBY_VERSION |
| 33 | + end |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + def ==(other) |
| 38 | + current == refined(other) |
| 39 | + end |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + def >(other) |
| 44 | + current > refined(other) |
| 45 | + end |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + def >=(other) |
| 50 | + current >= refined(other) |
| 51 | + end |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + def <(other) |
| 56 | + current < refined(other) |
| 57 | + end |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + def <=(other) |
| 62 | + current <= refined(other) |
| 63 | + end |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + # Returns the string filename for the current ruby version gemfile |
| 68 | + def gemfile |
| 69 | + if current == refined("2.7") |
| 70 | + "gemfiles/ruby-#{current.to_s.gsub('.','-')}/Gemfile" |
| 71 | + else |
| 72 | + "gemfiles/ruby-3/Gemfile" |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + |
| 77 | + # ====================================================================== |
| 78 | + # = Private |
| 79 | + # ====================================================================== |
| 80 | + |
| 81 | + # Return a Gem::Version from the given version refined down to MAJOR.MINOR segments |
| 82 | + # |
| 83 | + # version: anything that can be stringified with String(xxx) |
| 84 | + # |
| 85 | + def refined(version) |
| 86 | + version = String(version) |
| 87 | + refined_version = Gem::Version.new(version).release.segments[0..1].join('.') |
| 88 | + Gem::Version.new(refined_version) |
| 89 | + end |
| 90 | + end |
| 91 | +end |
0 commit comments