Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Ruby elegant way to provide default array values for version string

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

How about (min Ruby 2.6)

version_string.split('.').then { |x, y = '0', z = '0'| [x,y,z] }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!