Class | Gem::Version |
In: |
lib/rubygems/version.rb
|
Parent: | Object |
The Version class processes string versions into comparable values. A version string should normally be a series of numbers separated by periods. Each part (digits separated by periods) is considered its own number, and these are used for sorting. So for instance, 3.10 sorts higher than 3.2 because ten is greater than two.
If any part contains letters (currently only a-z are supported) then that version is considered prerelease. Versions with a prerelease part in the Nth part sort less than versions with N-1 parts. Prerelease parts are sorted alphabetically using the normal Ruby string sorting rules.
Prereleases sort between real releases (newest to oldest):
VERSION_PATTERN | = | '[0-9]+(\.[0-9a-z]+)*' |
version | [R] |
# File lib/rubygems/version.rb, line 76 76: def self.correct?(version) 77: pattern = /\A\s*(#{VERSION_PATTERN})*\s*\z/ 78: 79: version.is_a? Integer or 80: version =~ pattern or 81: version.to_s =~ pattern 82: end
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 92 92: def self.create(input) 93: if input.respond_to? :version then 94: input 95: elsif input.nil? then 96: nil 97: else 98: new input 99: end 100: end
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 106 106: def initialize(version) 107: raise ArgumentError, "Malformed version number string #{version}" unless 108: self.class.correct?(version) 109: 110: self.version = version 111: end
Compares this version with other returning -1, 0, or 1 if the other version is larger, the same, or smaller than this one.
# File lib/rubygems/version.rb, line 188 188: def <=>(other) 189: return nil unless self.class === other 190: return 1 unless other 191: mine, theirs = balance(self.parts.dup, other.parts.dup) 192: mine <=> theirs 193: end
# File lib/rubygems/version.rb, line 195 195: def balance(a, b) 196: a << Part.new(0) while a.size < b.size 197: b << Part.new(0) while b.size < a.size 198: [a, b] 199: end
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 are ignored. (e.g 5.3.1.b2 => 5.4)
# File lib/rubygems/version.rb, line 219 219: def bump 220: parts = parse_parts_from_version_string 221: parts.pop while parts.any? { |part| part.alpha? } 222: parts.pop if parts.size > 1 223: parts[-1] = parts[-1].succ 224: self.class.new(parts.join(".")) 225: end
Dump only the raw version string, not the complete object
# File lib/rubygems/version.rb, line 120 120: def marshal_dump 121: [@version] 122: end
Load custom marshal format
# File lib/rubygems/version.rb, line 127 127: def marshal_load(array) 128: self.version = array[0] 129: end
Strip ignored trailing zeros.
# File lib/rubygems/version.rb, line 138 138: def normalize 139: parts_arr = parse_parts_from_version_string 140: if parts_arr.length != 1 141: parts_arr.pop while parts_arr.last && parts_arr.last.value == 0 142: parts_arr = [Part.new(0)] if parts_arr.empty? 143: end 144: parts_arr 145: end
A version is considered a prerelease if any part contains a letter.
# File lib/rubygems/version.rb, line 166 166: def prerelease? 167: parts.any? { |part| part.alpha? } 168: end
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 173 173: def release 174: return self unless prerelease? 175: rel_parts = parts.dup 176: rel_parts.pop while rel_parts.any? { |part| part.alpha? } 177: self.class.new(rel_parts.join('.')) 178: end
Returns the text representation of the version
# File lib/rubygems/version.rb, line 150 150: def to_s 151: @version 152: end
# File lib/rubygems/version.rb, line 158 158: def version=(version) 159: @version = version.to_s.strip 160: normalize 161: end