Skip to content

Instantly share code, notes, and snippets.

@msepcot
Created May 22, 2013 18:35
Show Gist options
  • Save msepcot/5629828 to your computer and use it in GitHub Desktop.
Save msepcot/5629828 to your computer and use it in GitHub Desktop.
Add deep_find ability to Enumerable to recursively search for a Hash key. Returns the first key found.
module Enumerable
def deep_find(key)
if respond_to?(:key?) && key?(key)
self[key]
else
found = nil
find { |*a| v = a.last; found = v.is_a?(Enumerable) ? v.deep_find(key) : nil }
found
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment