Check if a hash's keys include all of a set of keys
%i[a b c d].all? {|s| hash.key? s}
Determine if hash contains any key from a set of keys?
Consider Enumerable.any?
[:a, :b, :e].any? {|k| h.key?(k)}
From IRB:
>> h = {:a => 1}; [:x].any? {|k| h.key?(k)}
=> false
>> h = {:a => 1}; [:x, :a].any? {|k| h.key?(k)}
=> true
Happy coding.
Testing if a hash has any of a number of keys
No need to loop:
(hash.keys & keys).any? # => true
Explanation:
.keys
returns all keys in a hash as an array. &
intersects two arrays, returning any objects that exists in both arrays. Finally, .any?
checks if the array intersect has any values.
How to check if a specific key is present in a hash or not?
Hash
's key?
method tells you whether a given key is present or not.
session.key?("user")
Check if a string includes any of the keys in a hash and return the value of the key it contains
I find this way readable:
hash_key_in_s = s[Regexp.union(h.keys)]
p h[hash_key_in_s] #=> "v1"
Or in one line:
p h.fetch s[Regexp.union(h.keys)] #=> "v1"
And here is a version not using regexp:
p h.fetch( h.keys.find{|key|s[key]} ) #=> "v1"
Check if two hashes have the same set of keys
Try:
# Check that both hash have the same number of entries first before anything
if h1.size == h2.size
# breaks from iteration and returns 'false' as soon as there is a mismatched key
# otherwise returns true
h1.keys.all?{ |key| !!h2[key] }
end
Enumerable#all?
worse case scenario, you'd only be iterating through the keys once.
Check if multiple key-value pairs exist in a Ruby hash?
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
ruby if hash keys include text, return hash key
Use find
:
if result = hash.keys.find {|k| k.include? number}
#do some work
end
How do I check if my hash has a key from an array of strings?
strings = ['a', 'b', 'c']
hash = {:a => 'apple', :b => 'bob', :d => 'thing'}
has_key = hash.keys.map(&:to_s) & strings # ['a', 'b']
has_key.any? # true
a one-liner that's similar, hash.keys.detect { |key| strings.include?(key.to_s) }.nil?
Find missing key in hash
You can make use of the Hash#keys
method
REQUIRED_KEYS = %w(from subject to body)
hash = {
"from"=>"abc@gmail.com",
"to"=>"def@gmail.com:ijk@gmail.com:lmn@gmail.com",
"subject"=>"hi",
"body"=>"there",
"cc" => "def@gmail.com:ijk@gmail.com",
"bcc" => "def@gmail.com:ijk@gmail.com"
}
REQUIRED_KEYS - hash.keys
#=> []
hash.delete('to')
#=> "def@gmail.com:ijk@gmail.com:lmn@gmail.com"
REQUIRED_KEYS - hash.keys
#=> ["to"]
Related Topics
Rails, How to Render a View/Partial in a Model
How to Log Every Method That's Called in a Ruby Program
Best Practice to Mark Deprecated Code in Ruby
Capistrano Asks for Password When Deploying, Despite Ssh Keys
Factory Girl - What's the Purpose
Upgrading from Rails 3 to Rails 3.1
Measure and Benchmark Time for Ruby Methods
How to Get Activerecord Associations via Reflection
How to Generate a Random Number Between a and B in Ruby
How to Update Ruby with Homebrew
Mailer Unable to Access Reset_Token in User Model