module Ruby::Version
Wraps the RUBY_VERSION
constant and provides some handling such as comparing.
Constants
Public Class Methods
Lower operator.
@param [String, Symbol, Array] value version identifier @return [Boolean] true
if yes, +false otherwise
# File lib/ruby-version.rb, line 93 def self.<(value) __cache(:<, value) do __compare(value, true, false, false) end end
Lower or equal operator.
@param [String, Symbol, Array] value version identifier @return [Boolean] true
if yes, +false otherwise
# File lib/ruby-version.rb, line 80 def self.<=(value) __cache(:"<=", value) do __compare(value, true, false, true) end end
Alias for {#compare}.
@param [String, Symbol, Array] value version identifier @return [Boolean] 1
if this one is higher, -1
if lower and 0
otherwise
# File lib/ruby-version.rb, line 145 def self.<=>(value) self.compare(value) end
Equality operator.
@param [String, Symbol, Array] value version identifier @return [Boolean] true
if yes, +false otherwise
# File lib/ruby-version.rb, line 119 def self.==(value) __cache(:"==", value) do __compare(value, false, false, true) end end
Higher operator.
@param [String, Symbol, Array] value version identifier @return [Boolean] true
if yes, +false otherwise
# File lib/ruby-version.rb, line 106 def self.>(value) __cache(:>, value) do __compare(value, false, true, false) end end
Higher or equal operator.
@param [String, Symbol, Array] value version identifier @return [Boolean] true
if yes, +false otherwise
# File lib/ruby-version.rb, line 67 def self.>=(value) __cache(:">=", value) do __compare(value, false, true, true) end end
Brokes string identifier to numeric tokens in array.
@param [String, Symbol] value version identifier @return [Array] array of tokens
# File lib/ruby-version.rb, line 156 def self.broke(string) string.to_s.split(".").map! { |i| i.to_i } end
Compares version strings.
@param [String, Symbol, Array] value version identifier @return [Boolean] 1
if this one is higher, -1
if lower and 0
otherwise
# File lib/ruby-version.rb, line 132 def self.compare(value) __cache(:compare, value) do __compare(value, -1, 1, 0) end end
Private Class Methods
Universal cache routine.
# File lib/ruby-version.rb, line 194 def self.__cache(operation, value, &block) value = value.to_sym if value.kind_of? String if @cache.nil? @cache = Hash::new { |dict, key| dict[key] = { } } end if not @cache[operation].has_key? value @cache[operation][value] = block.call() end @cache[operation][value] end
Universal comparing routine for all type of (in)equalities.
# File lib/ruby-version.rb, line 172 def self.__compare(value, lower, higher, equal) if not value.kind_of? Array value = self.broke(value.to_s) end self::TOKENS.each_index do |i| case self::TOKENS[i].to_i <=> value[i].to_i when -1 return lower when 1 return higher end end return equal end