| Class | Gem::Version |
| In: |
lib/rubygems/version.rb
|
| Parent: | Object |
The Version class processes string versions into comparable values
| ints | [R] | |
| version | [R] |
Returns true if version is a valid version string.
# File lib/rubygems/version.rb, line 23
23: def self.correct?(version)
24: case version
25: when Integer, /\A\s*(\d+(\.\d+)*)*\s*\z/ then true
26: else false
27: end
28: 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 38
38: def self.create(input)
39: if input.respond_to? :version then
40: input
41: elsif input.nil? then
42: nil
43: else
44: new input
45: end
46: end
Constructs a Version from the version string. A version string is a series of digits separated by dots.
# File lib/rubygems/version.rb, line 52
52: def initialize(version)
53: raise ArgumentError, "Malformed version number string #{version}" unless
54: self.class.correct?(version)
55:
56: self.version = version
57: 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 120
120: def <=>(other)
121: return nil unless self.class === other
122: return 1 unless other
123: @ints <=> other.ints
124: end
Return a new version object where the next to the last revision number is one greater. (e.g. 5.3.1 => 5.4)
# File lib/rubygems/version.rb, line 140
140: def bump
141: ints = build_array_from_version_string
142: ints.pop if ints.size > 1
143: ints[-1] += 1
144: self.class.new(ints.join("."))
145: end
Dump only the raw version string, not the complete object
# File lib/rubygems/version.rb, line 64
64: def marshal_dump
65: [@version]
66: end
Load custom marshal format
# File lib/rubygems/version.rb, line 69
69: def marshal_load(array)
70: self.version = array[0]
71: end
Strip ignored trailing zeros.
# File lib/rubygems/version.rb, line 76
76: def normalize
77: @ints = build_array_from_version_string
78:
79: return if @ints.length == 1
80:
81: @ints.pop while @ints.last == 0
82:
83: @ints = [0] if @ints.empty?
84: end
Returns the text representation of the version
| return: | [String] version as string |
# File lib/rubygems/version.rb, line 91
91: def to_s
92: @version
93: end
# File lib/rubygems/version.rb, line 107
107: def version=(version)
108: @version = version.to_s.strip
109: normalize
110: end