Skip to content
Browse files

Make HWIA copy the default proc too.

[fixes #16279]
  • Loading branch information...
1 parent 5f25435 commit c17c262cfcae307def3a0657602adccaf7fa91cf @arthurnn arthurnn committed Jul 24, 2014
View
4 activesupport/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Make `Hash#with_indifferent_access` copy the default proc too.
+
+ *arthurnn*, *Xanders*
+
* Add `String#truncate_words` to truncate a string by a number of words.
*Mohamed Osama*
View
1 activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -75,6 +75,7 @@ def self.new_from_hash_copying_default(hash)
hash = hash.to_hash
new(hash).tap do |new_hash|
new_hash.default = hash.default
+ new_hash.default_proc = hash.default_proc if hash.default_proc
end
end
View
11 activesupport/test/core_ext/hash_ext_test.rb
@@ -1543,6 +1543,17 @@ def test_should_copy_the_default_value_when_converting_to_hash_with_indifferent_
assert_equal 3, hash_wia.default
end
+ def test_should_copy_the_default_proc_when_converting_to_hash_with_indifferent_access
+ hash = Hash.new do
+ 2 + 1
+ end
+ assert_equal 3, hash[:foo]
+
+ hash_wia = hash.with_indifferent_access
+ assert_equal 3, hash_wia[:foo]
+ assert_equal 3, hash_wia[:bar]
+ end
+
# The XML builder seems to fail miserably when trying to tag something
# with the same name as a Kernel method (throw, test, loop, select ...)
def test_kernel_method_names_to_xml

0 comments on commit c17c262

Please sign in to comment.
Something went wrong with that request. Please try again.