Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#sunflower_recursive_merge!(other) ⇒ Object

Recursively, destructively merges two hashes that might contain further hashes and arrays. Hashes are merged using #merge!; arrays are merged using #concat.

Named like this to prevent monkey-patching conflicts; is a monkey-patch because it is convenient. Should be considered private to Sunflower and might disappear at any time. Used in Sunflower#API_continued.

From stackoverflow.com/a/2277713 , slightly modified.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sunflower/core.rb', line 15

def sunflower_recursive_merge!(other)
	other.keys.each do |k|
		if self[k].is_a?(Array) && other[k].is_a?(Array)
			self[k].concat(other[k])
		elsif self[k].is_a?(Hash) && other[k].is_a?(Hash)
			self[k].sunflower_recursive_merge!(other[k])
		else
			self[k] = other[k]
		end
	end
	self
end