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

221
Views
¿Cómo convertir una matriz de enteros de cadena en una matriz de enteros? Rubí

tengo una matriz:

 all_ages = ["11", "9", "10", "8", "9"]

y quiero convertirlo en una matriz de números enteros para que sea más fácil agregarlos todos juntos. Cualquier ayuda muy apreciada, buscando soluciones de nivel uno.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Se puede llamar a Enumerable#sum con un bloque que le dice a Ruby cómo sumar los elementos de la matriz, en este caso, llamar primero a String#to_i en el elemento.

 all_ages = ["11", "9", "10", "8", "9"] all_ages.sum(&:to_i) #=> 47
over 4 years ago · Santiago Trujillo Report

0

all_ages = ["11", "9", "10", "8", "9"]

Código

 p all_ages.map(&:to_i).sum

O

 p all_ages.map { |x| x.to_i }.sum

Producción

 47
over 4 years ago · Santiago Trujillo Report

0

Aquí hay un enfoque diferente que también puede ser útil para agregar a su arsenal:

 all_ages = ["11", "9", "10", "8", "9"] def sum_str_to_i (array) total = 0 array.each {|x| total += x.to_i} total end sum_str_to_i(all_ages) #=> 47

O, si no desea definir un nuevo método:

 total = 0 all_ages.each {|x| total += x.to_i} total #=> 47
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!