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

306
Views
Ruby creando un nuevo hash a partir de una matriz de clave, valor
first_response = [ {"xId" => "123", "yId" => "321"}, {"xId" => "x", "yId" => "y" } ] first_response.each do |resp| x_id = resp['xId'] y_id = resp['yId'] puts x_id.to_s puts y_id.to_s end

Esto me da salidas

 123 321 x y

el hash de salida que quiero crear es {123=>{321}, x=>{y}}

primer servicio: tengo una matriz de hash que tiene dos identificadores diferentes, ejemplo: (x_id y y_id) (habría múltiples pares como ese en la respuesta)

Quiero crear un hash que contenga el par coincidente de x_id e y_ids que obtenemos del primer servicio con x_id como clave para todos los pares.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Si sabe que cada hash en first_response va a contener exactamente dos pares clave/valor, puede extraer sus valores y luego convertir ese resultado en un hash (ver Enumerable#to_h ):

 first_response.to_h(&:values) # {"123"=>"321", "x"=>"y"}
over 4 years ago · Santiago Trujillo Report

0

Parece que este enfoque funciona, pero no estoy completamente seguro de si es correcto.

 first_response = [{"xId"=>"123","yId"=> "321"}, {"xId"=>"x","yId"=> "y"}] h = {}.tap do |element| first_response.each do |resp| x_id = resp['xId'] y_id = resp['yId'] element[x_id] = y_id end end puts h.to_s # {"123"=>"321", "x"=>"y"}
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!