module Ruby::Version

Wraps the RUBY_VERSION constant and provides some handling such as comparing.

Constants

TOKENS

Contents frozen tokens array of the current ruby version. @see VERSION

VERSION

Contains the Ruby version identification in frozen string. It’s content is equal to the RUBY_VERSION constant.

Public Class Methods

<(value) click to toggle source

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
<=(value) click to toggle source

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
<=>(value) click to toggle source

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
==(value) click to toggle source

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
>(value) click to toggle source

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
>=(value) click to toggle source

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
broke(string) click to toggle source

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
compare(value) click to toggle source

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

__cache(operation, value, &block) click to toggle source

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
__compare(value, lower, higher, equal) click to toggle source

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