This is a trivial question, unsure if I should really be asking here, but would like to see if anyone has a more elegant way to write this in ruby.
I have a version string of format x.y.z or x.y or x. I would like a simple elegant way to convert this into an array where default 0 value is inserted if segment is missing. I have a few different ways to do this, but I was hoping for something a bit cleaner. Atm my current solution is this
version_string.split('.').each_with_index.with_object(['0','0','0']) { |(segment, i), version_array| version_array[i] = segment }
Seems to work fine, and I can always move to a simple method call to make code look cleanup, but something about the use of each_with_index and `with_object kinda bugs me. Just curious to see if rest of Ruby community have anything to add
How about (min Ruby 2.6)
version_string.split('.').then { |x, y = '0', z = '0'| [x,y,z] }