From d74783e78e9620b1d7f6c2abefdcfee5b1261355 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 12 Sep 2009 02:22:12 +0200 Subject: AS guide: documents Hash#deep_merge --- .../guides/source/active_support_overview.textile | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'railties') diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index a4866f18be..ce548f28d2 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -1110,6 +1110,27 @@ By default the root node is "hash", but that's configurable via the :rootBuilder::XmlMarkup. You can configure your own builder with the :builder option. The method also accepts options like :dasherize and friends, they are forwarded to the builder. +h4. Deep Merging + +Ruby has a builtin method +Hash#merge+ that merges two hashes: + + +{:a => 1, :b => 1}.merge(:a => 0, :c => 2) +# => {:a => 0, :b => 1, :c => 2} + + +As you can see in the previous example if a key is found in both hashes the value in the one in the argument wins. + +Active Support defines +Hash#deep_merge+. In a deep merge, if a key is found in both hashes and their values are hashes in turn, then their _merge_ becomes the value in the resulting hash: + + +{:a => {:b => 1}}.deep_merge(:a => {:c => 2}) +# => {:a => {:b => 1, :c => 2}} + + +The method +deep_merge!+ performs a deep merge in place. + + h3. Extensions to +Range+ ... -- cgit v1.2.3