Class: Version

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

Overview

This class stores numbers with multiple decimal points, a format commonly used for version numbers. For example ‘2.5.1’.

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Returns a new instance of Version.



7
8
9
# File 'lib/versiontype.rb', line 7

def initialize(version)
  @version = version.to_s
end

Instance Method Details

#<=>(other) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/versiontype.rb', line 15

def <=>(other)
  ourfields = @version.split('.')
  otherfields = other.to_s.split('.')
  # Convert anything like '.5' to '0.5'
  # '.5'.split('.') returns ['', '5']
  [ourfields, otherfields].each do |fields|
    if fields[0] == ''
      fields[0] = '0'
    end
  end
  
  convert_and_split!(ourfields, otherfields)
  
  # Array conveniently implements <=>
  ourfields <=> otherfields
end

#to_sObject



11
12
13
# File 'lib/versiontype.rb', line 11

def to_s
  @version
end