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

230
Visualizações
Check if multiple key-value pairs exist in a Ruby hash?

I have the following Ruby hash key-value pairs:

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

What I need to check is see if the following key-value pairs are both present:

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

Essentially wanting something to the effect of...

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 Respostas
Responde à pergunta

0

If you are using ruby >= 2.3, there is a fancy new Hash >= Hash operation that is conceptually similiar to a hypothetical contains?

Using your traits array:

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

trait >= {"trait_type" => "Status", "value" => "Built"}
# => false

So you could try something like:

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

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" }

Here are three solutions.

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 Relatório

0

We also have the aptly named all? and any? methods that should do exactly what you're looking for in a very logical way.

All we need to do is loop through your filter_traits hash and test to see if all (or any) those key:value pairs are equal to (==) any corresponding key:value pairs inside your traits array:

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 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