I have a hash that has some keys as an array like so:
foo = {[45, 121]=>:some_field}
How can I select :some_field where a foo key contains 45?
And secondary to that, if it finds a match, how do I retrieve the other elements in the same key?
Although you can do this, it kind of defeats the purpose of using a hash since you will have to do a linear scan through the entire thing. It would be a lot better to have multiple hash keys for the same value since you can use the hash as an index then.
Example:
found = foo.find { |k, v| k.include?(n) }
found and found[1]
Keep in mind the performance of this will be terrible if you have large numbers of entries in the key and a large number of items in the hash since it will have to test against all keys and all values individually.
foo = {[45, 121]=>:some_field}
foo.detect{ |k,v| k.include? 45 }
#=> [[45, 121], :some_field]
foo.detect{ |k,v| k.include? 45 }.last
#=> :some_field
:some_fieldwould be the key, and the value would be an array to which you append additional numbers. I can't say for certain that it you're using it incorrectly as there is absolutely no context, but anarray=>symbolhash is not often as useful assymbol=>array.