Created
May 22, 2013 18:35
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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