Reputation: 5774
Given a hash -
hash = {
1 => {"ID" => ["NUMBER", 11] },
2 => {"TITLE" => ["VARCHAR2", 5] },
3 => {"FIRST_NAME" => ["VARCHAR2", 50] },
4 => {"LAST_NAME" => ["VARCHAR2", 50] },
5 => {"BIRTH_DATE" => ["DATE", -2] }
}
and 2 input parameters - "FIRST_NAME"
and ["VARCHAR2",50]
.
What is the most elegant way to do -
"FIRST_NAME"
exists as key of any nested hash. hash[3]["FIRST_NAME"]
is equal to second parameter i.e. ["VARCHAR2",50]
. Currently I do the following -
array = hash.values.map {|h| h.to_a}.flatten(2)
puts hash.key(Hash["FIRST_NAME",["VARCHAR2",50]]) if !(index = array.index("FIRST_NAME")).nil? ? array[index+1] == ["VARCHAR2",50] : false # 3
Upvotes: 1
Views: 2555
Reputation: 15010
You will find a lot of information in you check the hash api
.
a = 'FIRST_NAME'
b = ['VARCHAR2', 50]
hash.each do |k, v|
if v.key?(a) # ①
return k if v[a] == b # ② and ③
end
end
You could also write it as below actually because if it does not contain the key, it will never match and just go to the next occurence.
hash.each do |k, v|
return k if v[a] == b
end
Your example only show hash with one pair, if there is more your need to do a new each
on v
.
Upvotes: 1
Reputation: 168269
There is a method key
for doing just that:
hash.key({"FIRST_NAME" => ["VARCHAR2",50]}) # => 3
Upvotes: 1
Reputation: 21116
Seems like what your doing defeats the purpose/performance of a hash.
You should be using the (I assume) unique names "ID", "TITLE", "FIRST_NAME"... as your top level keys; the numbers 1-5 seem unused.
Searching a hash map by key is fast, having to map all values into an array than do a linear search on the array is slow.
Upvotes: 1