Class: Gem::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rubygems/version.rb

Overview

The Version class processes string versions into comparable values

Defined Under Namespace

Classes: Requirement

Constant Summary collapse

NUM_RE =
/\s*(\d+(\.\d+)*)*\s*/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Constructs a version from the supplied string

version
String

The version string. Format is digit.digit…

Raises:

  • (ArgumentError)


115
116
117
118
119
# File 'lib/rubygems/version.rb', line 115

def initialize(version)
  raise ArgumentError, 
    "Malformed version number string #{version}" unless Version.correct?(version)
  @version = version
end

Instance Attribute Details

#versionObject

Returns the value of attribute version.



78
79
80
# File 'lib/rubygems/version.rb', line 78

def version
  @version
end

Class Method Details

.correct?(str) ⇒ Boolean

Checks if version string is valid format

str
String

the version string

return
Boolean

true if the string format is correct, otherwise false

Returns:

  • (Boolean)


88
89
90
# File 'lib/rubygems/version.rb', line 88

def self.correct?(str)
  /^#{NUM_RE}$/.match(str)
end

.create(input) ⇒ Object

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


100
101
102
103
104
105
106
107
108
# File 'lib/rubygems/version.rb', line 100

def self.create(input)
  if input.respond_to? :version
    return input
  elsif input.nil?
    return nil
  else
    return Version.new(input)
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Compares two versions

other
Version or .to_ints

other version to compare to

return
Fixnum

-1, 0, 1



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rubygems/version.rb', line 145

def <=>(other)
  return 1 unless other
  rnums, vnums = to_ints, other.to_ints
  [rnums.size, vnums.size].max.times {|i|
    rnums[i] ||= 0
    vnums[i] ||= 0
  }
  
  begin
    r,v = rnums.shift, vnums.shift
  end until (r != v || rnums.empty?)

  return r <=> v
end

#bumpObject

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



166
167
168
169
170
171
# File 'lib/rubygems/version.rb', line 166

def bump
  ints = to_ints
  ints.pop if ints.size > 1
  ints[-1] += 1
  self.class.new(ints.join("."))
end

#hashObject



160
161
162
# File 'lib/rubygems/version.rb', line 160

def hash
  to_ints.inject { |hash_code, n| hash_code + n }
end

#to_intsObject

Convert version to integer array

return
Array

list of integers



135
136
137
# File 'lib/rubygems/version.rb', line 135

def to_ints
  @version.scan(/\d+/).map {|s| s.to_i}
end

#to_sObject

Returns the text representation of the version

return
String

version as string



126
127
128
# File 'lib/rubygems/version.rb', line 126

def to_s
  @version
end