Module: Pantheios::Util::VersionUtil

Defined in:
lib/pantheios/util/version_util.rb

Overview

version utilities

Class Method Summary collapse

Class Method Details

.version_compare(lhs, rhs) ⇒ Object

Compares two version designators and returns a spaceship comparison result

Signature

  • Parameters:

  • lhs [String, Array] The left-hand comparand

  • rhs [String, Array] The right-hand comparand

  • Returns:

  • 0 if the two version designators represent exactly the same version

  • <0 if the lhs version designator represents an earlier version than the rhs version designator

  • >0 if the lhs version designator represents a later version than the rhs version designator



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pantheios/util/version_util.rb', line 23

def self.version_compare lhs, rhs

  lhs  =  lhs.split('.') if String === lhs
  rhs  =  rhs.split('.') if String === rhs

  lhs  =  lhs.map { |n| n.to_i }
  rhs  =  rhs.map { |n| n.to_i }

  if lhs.size < rhs.size

    lhs += [ 0 ] * (rhs.size - lhs.size)
  else

    rhs += [ 0 ] * (lhs.size - rhs.size)
  end

  lhs <=> rhs
end