Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
require 'minitest/autorun'
require 'minitest/emoji'
class Version
def self.next_version version
next_version_3 version
end
def self.next_version_1 version
number_as_string = version.split(".").join('')
string_length = number_as_string.length
next_version_string = (number_as_string.to_i+1).to_s
if next_version_string.length==string_length
next_version_string.split('').join(".")
else
next_version_string[0..1] + "." + next_version_string[2..-1].split('').join(".")
end
end
def self.next_version_2 version
number_as_string = version.split(".").join('')
starts_with_zero = number_as_string[0].to_i == 0
string_length = number_as_string.length
next_version_string = (number_as_string.to_i+1).to_s
if next_version_string.length > string_length
next_version_string[0..1] + "." + next_version_string[2..-1].split('').join(".")
else
next_version_string = "0" + next_version_string if starts_with_zero && next_version_string.length < string_length
next_version_string.split('').join(".")
end
end
def self.next_version_3 version
version_array = version.split(".").reverse.map(&:to_i)
version_array[0] = version_array.first + 1
version_array = version_array.each_with_index.map do |n,index|
if index == version_array.size - 1
n
else
if n > 9
version_array[index+1]=version_array[index+1]+1
0
else
n
end
end
end
version_array.reverse.join(".")
end
end
# tests
class VersionTest < Minitest::Test
def test_1_2_3
assert_equal "1.2.4", Version.next_version("1.2.3")
end
def test_0_9_9
assert_equal "1.0.0", Version.next_version("0.9.9")
end
def test_1
assert_equal "2", Version.next_version("1")
end
def test_1_2_3_4_5_6_7_8
assert_equal "1.2.3.4.5.6.7.9", Version.next_version("1.2.3.4.5.6.7.8")
end
def test_9_9
assert_equal "10.0", Version.next_version("9.9")
end
def test_0_8_7_9
assert_equal "0.8.8.0", Version.next_version("0.8.7.9")
end
def test_13_9_9_9_9_9
assert_equal "14.0.0.0.0.0", Version.next_version("13.9.9.9.9.9")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment