Created
October 6, 2021 08:50
-
-
Save 1997roylee/12be09653e56d0bbdf95ff0963eeec17 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
module DeepFindHelper | |
def deep_find(hash, key) | |
return nil unless hash.is_a?(Hash) | |
hash.filter do |k, v| | |
if v.is_a?(Hash) | |
deep_find(v, key) | |
else | |
k == key | |
end | |
end | |
end | |
module_function :deep_find | |
refine Hash do | |
include DeepFindHelper | |
def deep_find(key) | |
DeepFindHelper.deep_find(self, key) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment