Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/fetch_nested.rb

Instance Method Summary collapse

Instance Method Details

#fetch_nested(search_value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fetch_nested.rb', line 3

def fetch_nested(search_value)
	array = []
	array2 = []
	if self.has_key?(search_value)
		return self.fetch(search_value)
	else
		array = self.values
		if array[0].class == Hash
			array.each do |item|
				if item.has_key?(search_value)
					return item.fetch(search_value)
				end
			end
			array.each do |item|
				array2 << item.values
			end
			array2.flatten!
			if array2[0].class == Hash
				array2.each do |item|
					if item.has_key?(search_value)
						return item.fetch(search_value)
					end
				end
			end
		end
	end
	return nil
end