Skip to content

Commit

Permalink
Merge pull request #6321 from frodsan/backport_docs
Browse files Browse the repository at this point in the history
Backporting docs
  • Loading branch information
vijaydev committed May 15, 2012
2 parents e9051e2 + b2f73c4 commit 007539d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
7 changes: 7 additions & 0 deletions activesupport/lib/active_support/core_ext/hash/deep_dup.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class Hash
# Returns a deep copy of hash.
#
# hash = { :a => { :b => 'b' } }
# dup = hash.deep_dup
# dup[:a][:c] = 'c'
#
# hash[:a][:c] #=> nil
# dup[:a][:c] #=> "c"
def deep_dup
duplicate = self.dup
duplicate.each_pair do |k,v|
Expand Down
9 changes: 7 additions & 2 deletions activesupport/lib/active_support/core_ext/hash/deep_merge.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
class Hash
# Returns a new hash with +self+ and +other_hash+ merged recursively.
#
# h1 = {:x => {:y => [4,5,6]}, :z => [7,8,9]}
# h2 = {:x => {:y => [7,8,9]}, :z => "xyz"}
#
# h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" }
# h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] }
def deep_merge(other_hash)
dup.deep_merge!(other_hash)
end

# Returns a new hash with +self+ and +other_hash+ merged recursively.
# Modifies the receiver in place.
# Same as +deep_merge+, but modifies +self+.
def deep_merge!(other_hash)
other_hash.each_pair do |k,v|
tv = self[k]
Expand Down
11 changes: 9 additions & 2 deletions activesupport/lib/active_support/core_ext/hash/keys.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
class Hash
# Return a new hash with all keys converted to strings.
#
# { :name => 'Rob', :years => '28' }.stringify_keys
# #=> { "name" => "Rob", "years" => "28" }
def stringify_keys
dup.stringify_keys!
end

# Destructively convert all keys to strings.
# Destructively convert all keys to strings. Same as
# +stringify_keys+, but modifies +self+.
def stringify_keys!
keys.each do |key|
self[key.to_s] = delete(key)
Expand All @@ -14,12 +18,15 @@ def stringify_keys!

# Return a new hash with all keys converted to symbols, as long as
# they respond to +to_sym+.
#
# { 'name' => 'Rob', 'years' => '28' }.symbolize_keys
# #=> { :name => "Rob", :years => "28" }
def symbolize_keys
dup.symbolize_keys!
end

# Destructively convert all keys to symbols, as long as they respond
# to +to_sym+.
# to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
def symbolize_keys!
keys.each do |key|
self[(key.to_sym rescue key) || key] = delete(key)
Expand Down

0 comments on commit 007539d

Please sign in to comment.