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

236
Views
¿Comprobar si existen varios pares clave-valor en un hash de Ruby?

Tengo los siguientes pares clave-valor de hash de Ruby:

 [ { "trait_type"=>"Status", "value"=>"Unbuilt", "display_type"=>nil, "max_value"=>nil, "trait_count"=>4866, "order"=>nil } ]

Lo que necesito verificar es ver si los siguientes pares clave-valor están presentes:

 { "value"=>"Unbuilt", "trait_type"=>"Status" }

Esencialmente querer algo en el sentido de...

 traits = [{"trait_type"=>"Status", "value"=>"Unbuilt", "display_type"=>nil, "max_value"=>nil, "trait_count"=>4866, "order"=>nil}] filter_traits = {"value"=>"Unbuilt", "trait_type"=>"Status"} traits.include? filter_traits
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Si está utilizando Ruby >= 2.3, hay una nueva y elegante operación Hash >= Hash que es conceptualmente similar a un contenedor hipotético contains?

Usando su matriz de traits :

 trait = traits[0] trait >= {"trait_type" => "Status", "value" => "Unbuilt"} # => true trait >= {"trait_type" => "Status", "value" => "Built"} # => false

Así que podrías intentar algo como:

 traits.select{|trait| trait >= filter_traits }.length > 0 # => true
over 4 years ago · Santiago Trujillo Report

0

arr = [ { "trait_type"=>"Status", "value"=>"Unbuilt", "display_type"=>nil, "max_value"=>nil, "trait_count"=>4866, "order"=>nil } ]
 h1 = { "value"=>"Unbuilt", "trait_type"=>"Status" } h2 = { "value"=>"Rebuilt", "trait_type"=>"Status" }

Aquí hay tres soluciones.

 arr[0].slice(*h1.keys) == h1 #=> true arr[0].slice(*h2.keys) == h2 #=> false

 arr[0].values_at(h1.keys) == h1.values #=> true arr[0].values_at(h2.keys) == h2.values #=> false

 arr[0] == arr[0].merge(h1) #=> true arr[0] == arr[0].merge(h2) #=> false
over 4 years ago · Santiago Trujillo Report

0

También tenemos el acertadamente llamado all? y any? métodos que deberían hacer exactamente lo que está buscando de una manera muy lógica.

Todo lo que tenemos que hacer es recorrer el hash filter_traits y probar si todos (o alguno) de esos pares key:value son iguales a ( == ) cualquier par clave:valor correspondiente dentro de su matriz de traits :

 traits = [{"trait_type"=>"Status", "value"=>"Unbuilt", "display_type"=>nil, "max_value"=>nil, "trait_count"=>4866, "order"=>nil}] filter_traits = {"value"=>"Unbuilt", "trait_type"=>"Status"} filter_traits.all? {|k, v| filter_traits[k] == traits[0][k]} #=> true filter_traits = {"value"=>"Built", "trait_type"=>"Status"} filter_traits.all? {|k, v| filter_traits[k] == traits[0][k]} #=> false filter_traits.any? {|k, v| filter_traits[k] == traits[0][k]} #=> true
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!