Class: Gem::Version
- Inherits:
-
Object
- Object
- Gem::Version
- 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
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
-
.correct?(str) ⇒ Boolean
Checks if version string is valid format.
-
.create(input) ⇒ Object
Factory method to create a Version object.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares two versions.
-
#bump ⇒ Object
Return a new version object where the next to the last revision number is one greater.
- #hash ⇒ Object
-
#initialize(version) ⇒ Version
constructor
Constructs a version from the supplied string.
-
#to_ints ⇒ Object
Convert version to integer array.
-
#to_s ⇒ Object
Returns the text representation of the version.
Constructor Details
#initialize(version) ⇒ Version
Constructs a version from the supplied string
- version
- String
-
The version string. Format is digit.digit…
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
#version ⇒ Object
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
88 89 90 |
# File 'lib/rubygems/version.rb', line 88 def self.correct?(str) /^#{NUM_RE}$/.match(str) 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 |
#bump ⇒ Object
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 |
#hash ⇒ Object
160 161 162 |
# File 'lib/rubygems/version.rb', line 160 def hash to_ints.inject { |hash_code, n| hash_code + n } end |
#to_ints ⇒ Object
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_s ⇒ Object
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 |