class Gem::Version

Public Class Methods

correct?(version) click to toggle source

True if the version string matches RubyGems’ requirements.

# File lib/rubygems/version.rb, line 174
def self.correct?(version)
  unless Gem::Deprecate.skip
    warn "nil versions are discouraged and will be deprecated in Rubygems 4" if version.nil?
  end

  !!(version.to_s =~ ANCHORED_VERSION_PATTERN)
end
create(input) click to toggle source

Factory method to create a Version object. Input may be a Version or a String. Intended to simplify client code.

ver1 = Version.create('1.3.17')   # -> (Version object)
ver2 = Version.create(ver1)       # -> (ver1)
ver3 = Version.create(nil)        # -> nil
# File lib/rubygems/version.rb, line 190
def self.create(input)
  if self === input # check yourself before you wreck yourself
    input
  elsif input.nil?
    nil
  else
    new input
  end
end
new(version) click to toggle source

Constructs a Version from the version string. A version string is a series of digits or ASCII letters separated by dots.

# File lib/rubygems/version.rb, line 214
def initialize(version)
  unless self.class.correct?(version)
    raise ArgumentError, "Malformed version number string #{version}"
  end

  # If version is an empty string convert it to 0
  version = 0 if version.is_a?(String) && version =~ /\A\s*\Z/

  @version = version.to_s.strip.gsub("-",".pre.")
  @segments = nil
end

Public Instance Methods

<=>(other) click to toggle source

Compares this version with other returning -1, 0, or 1 if the other version is larger, the same, or smaller than this one. Attempts to compare to something that’s not a Gem::Version return nil.

# File lib/rubygems/version.rb, line 346
def <=>(other)
  return unless Gem::Version === other
  return 0 if @version == other._version || canonical_segments == other.canonical_segments

  lhsegments = canonical_segments
  rhsegments = other.canonical_segments

  lhsize = lhsegments.size
  rhsize = rhsegments.size
  limit  = (lhsize > rhsize ? lhsize : rhsize) - 1

  i = 0

  while i <= limit
    lhs, rhs = lhsegments[i] || 0, rhsegments[i] || 0
    i += 1

    next      if lhs == rhs
    return -1 if String  === lhs && Numeric === rhs
    return  1 if Numeric === lhs && String  === rhs

    return lhs <=> rhs
  end

  return 0
end
approximate_recommendation() click to toggle source

A recommended version for use with a ~> Requirement.

# File lib/rubygems/version.rb, line 328
def approximate_recommendation
  segments = self.segments

  segments.pop    while segments.any? {|s| String === s }
  segments.pop    while segments.size > 2
  segments.push 0 while segments.size < 2

  recommendation = "~> #{segments.join(".")}"
  recommendation += ".a" if prerelease?
  recommendation
end
bump() click to toggle source

Return a new version object where the next to the last revision number is one greater (e.g., 5.3.1 => 5.4).

Pre-release (alpha) parts, e.g, 5.3.1.b.2 => 5.4, are ignored.

# File lib/rubygems/version.rb, line 232
def bump
  @@bump[self] ||= begin
                     segments = self.segments
                     segments.pop while segments.any? {|s| String === s }
                     segments.pop if segments.size > 1

                     segments[-1] = segments[-1].succ
                     self.class.new segments.join(".")
                   end
end
canonical_segments() click to toggle source
# File lib/rubygems/version.rb, line 373
def canonical_segments
  @canonical_segments ||=
    _split_segments.map! do |segments|
      segments.reverse_each.drop_while {|s| s == 0 }.reverse
    end.reduce(&:concat)
end
eql?(other) click to toggle source

A Version is only eql? to another version if it’s specified to the same precision. Version “1.0” is not the same as version “1”.

# File lib/rubygems/version.rb, line 247
def eql?(other)
  self.class === other and @version == other._version
end
freeze() click to toggle source
Calls superclass method Object#freeze
# File lib/rubygems/version.rb, line 380
def freeze
  prerelease?
  canonical_segments
  super
end
marshal_dump() click to toggle source

Dump only the raw version string, not the complete object. It’s a string for backwards (RubyGems 1.3.5 and earlier) compatibility.

# File lib/rubygems/version.rb, line 267
def marshal_dump
  [version]
end
marshal_load(array) click to toggle source

Load custom marshal format. It’s a string for backwards (RubyGems 1.3.5 and earlier) compatibility.

# File lib/rubygems/version.rb, line 275
def marshal_load(array)
  initialize array[0]
end
prerelease?() click to toggle source

A version is considered a prerelease if it contains a letter.

# File lib/rubygems/version.rb, line 296
def prerelease?
  unless instance_variable_defined? :@prerelease
    @prerelease = !!(@version =~ /[a-zA-Z]/)
  end
  @prerelease
end
release() click to toggle source

The release for this version (e.g. 1.2.0.a -> 1.2.0). Non-prerelease versions return themselves.

# File lib/rubygems/version.rb, line 311
def release
  @@release[self] ||= if prerelease?
                        segments = self.segments
                        segments.pop while segments.any? {|s| String === s }
                        self.class.new segments.join('.')
                      else
                        self
                      end
end
to_s()
Alias for: version
version() click to toggle source

A string representation of this Version.

# File lib/rubygems/version.rb, line 165
def version
  @version.dup
end
Also aliased as: to_s

Protected Instance Methods

_segments() click to toggle source
# File lib/rubygems/version.rb, line 392
def _segments
  # segments is lazy so it can pick up version values that come from
  # old marshaled versions, which don't go through marshal_load.
  # since this version object is cached in @@all, its @segments should be frozen

  @segments ||= @version.scan(/[0-9]+|[a-z]+/i).map do |s|
    /^\d+$/ =~ s ? s.to_i : s
  end.freeze
end
_split_segments() click to toggle source
# File lib/rubygems/version.rb, line 402
def _split_segments
  string_start = _segments.index {|s| s.is_a?(String) }
  string_segments = segments
  numeric_segments = string_segments.slice!(0, string_start || string_segments.size)
  return numeric_segments, string_segments
end
_version() click to toggle source
# File lib/rubygems/version.rb, line 388
def _version
  @version
end