Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda