class HashDeepSearch
Public Class Methods
new(hash)
click to toggle source
# File lib/hash_deep_search.rb, line 3 def initialize(hash) @hash = hash end
Public Instance Methods
search(key_to_find)
click to toggle source
# File lib/hash_deep_search.rb, line 7 def search(key_to_find) if @hash.has_key?(key_to_find) return {key_to_find => @hash[key_to_find]} else @hash.keys.each do |existing_key| case @hash[existing_key] when Hash search = HashDeepSearch.new(@hash[existing_key]) result = search.search(key_to_find) return result if result.is_a?(Hash) when Array @hash[existing_key].each do |array_item| case array_item when Hash search = HashDeepSearch.new(array_item) result = search.search(key_to_find) return result if result.is_a?(Hash) else next end end else next end end end end