Created
December 8, 2009 17:10
-
-
Save valakirka/251806 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
Hash.class_eval do | |
def deep_search(key) | |
res=[] | |
res << self[key] | |
values.select{|v| v.is_a?(Hash) || v.is_a?(Array)}.each do |ha| | |
children=ha.deep_search(key) | |
res+= children if children | |
end | |
res.compact! | |
res.empty? ? [] : res | |
end | |
end | |
Array.class_eval do | |
def deep_search(key) | |
self.find_all{|x| x.respond_to? :deep_search}.map{|ha| ha.deep_search(key)}.inject([]){|array,item| array+item} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment