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

229
Views
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 answers
Answer question

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

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 Report

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