Skip to content
Browse files

Merge pull request #12611 from antonio/slice_bang_honor_default_proc

Make slice! honor default hash value/proc
Conflicts:
	activesupport/CHANGELOG.md
  • Loading branch information...
1 parent 961f00a commit e5e305ea71505ea61b37e00b6a346a767f8b0af1 @rafaelfranca rafaelfranca committed
View
4 activesupport/CHANGELOG.md
@@ -1,5 +1,9 @@
## unreleased ##
+* Fix `slice!` deleting the default value of the hash.
+
+ *Antonio Santos*
+
* Disable the ability to iterate over Range of AS::TimeWithZone
due to significant performance issues.
View
2 activesupport/lib/active_support/core_ext/hash/slice.rb
@@ -26,6 +26,8 @@ def slice!(*keys)
keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
omit = slice(*self.keys - keys)
hash = slice(*keys)
+ hash.default = default
+ hash.default_proc = default_proc if default_proc
replace(hash)
omit
end
View
18 activesupport/test/core_ext/hash_ext_test.rb
@@ -751,6 +751,24 @@ def test_indifferent_slice_access_with_symbols
assert_equal 'bender', slice['login']
end
+ def test_slice_bang_does_not_override_default
+ hash = Hash.new(0)
+ hash.update(a: 1, b: 2)
+
+ hash.slice!(:a)
+
+ assert_equal 0, hash[:c]
+ end
+
+ def test_slice_bang_does_not_override_default_proc
+ hash = Hash.new { |h, k| h[k] = [] }
+ hash.update(a: 1, b: 2)
+
+ hash.slice!(:a)
+
+ assert_equal [], hash[:c]
+ end
+
def test_extract
original = {:a => 1, :b => 2, :c => 3, :d => 4}
expected = {:a => 1, :b => 2}

0 comments on commit e5e305e

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