This is a question about pattern matching in Ruby 3.
I have a hash:
h = {
x: [1, 2, 3],
y: [11, 12, 13],
z: [100, 101],
}
Given an integer (for example, 13
), I'd like find the hash key whose hash value contains the integer (:y
in the example).
Of course, there're ways to do this in Ruby without using pattern matching.
Here, I'm ONLY interested in using pattern matching of Ruby 3 on the hash to achieve this. Typically, pattern matching in Ruby matches on the keys of a hash, not on the values. Also, this hash has many keys and we don't know in advance the key names.
The nearest thing I've found is by converting the hash into an array first and using pattern matching on the array.
def find_the_key(a_hash, a_number)
case a_hash.to_a
in [*, [the_key, [*, ^a_number, *]], *]
the_key
else
:key_not_found
end
end
puts find_the_key(h, 13) => :y
puts find_the_key(h, 999) => :key_not_found
Is there a way to make this work by using pattern matching on the hash itself, ie, without converting into an array first?
Are there other ways of applying pattern matching to solve this problem?
Thank you