Skip to content

Instantly share code, notes, and snippets.

@valakirka
Created December 8, 2009 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valakirka/251806 to your computer and use it in GitHub Desktop.
Save valakirka/251806 to your computer and use it in GitHub Desktop.
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