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

146
Views
Cómo asignar una matriz anidada al segundo atributo de la matriz en Ruby

Perdón por el título pobre, simplemente no sé cómo expresar lo que quiero aquí sin ejemplos.

tengo lo siguiente

 [ [ [1,2], true ], [ [3,4], false ] ]

y quiero conseguir

 [ [1, true], [2, true], [3, false], [4, false] ]

¿Hay alguna forma limpia de hacer esto en Ruby?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede usar una combinación de flat_map en la matriz externa y map en la matriz interna. Algo como:

 arr = [ [ [1,2], true ], [ [4,5], false ] ] arr.flat_map do |nums, val| nums.map {|num| [num, val]} end
over 4 years ago · Santiago Trujillo Report

0

Podrías usar Array#map y Array#product :

 arr = [ [ [1,2], true ], [ [3,4], false ] ].flat_map { |(arr, bool)| arr.product([bool]) } # [[1, true], [2, true], [3, false], [4, false]]
over 4 years ago · Santiago Trujillo Report

0

Usando Enumerable#each_with_object

 ary = [ [[1, 2], true], [[3, 4], false] ] ary.each_with_object([]) do |(nums, boolean), new_ary| nums.each { |num| new_ary << [num, boolean] } end # => [[1, true], [2, true], [3, false], [4, false]]
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!