Mega Code Archive

 
Categories / Ruby / Hash
 

Inject with regular expression

h = { "apple tree" => "plant", "ficus" => "plant",       "shrew" => "animal", "plesiosaur" => "animal" } h.inject([]) { |res, kv| res << kv if kv[1] =~ /p/; res } p h # => [["ficus", "plant"], ["apple tree", "plant"]]