grep defined on Enumerable , that is, it is a generic method that knows nothing about Hash es. It works on all Enumerable elements. Ruby is not a type for key-value pairs; it simply represents Hash entries as two-element arrays, where the first element is the key and the second element is the value.
grep uses the === method to filter items. And no matter how
/a/ === ['a', 'b']
neither
/b/ === ['a', 'b']
are true, you always get an empty array as an answer.
Try the following:
def (t = Object.new).===(other) true end {'a' => 'b'}.grep t
Here you can see how grep works with Hash es.
JΓΆrg W Mittag
source share