Permalink
Browse files

Merge pull request #12611 from antonio/slice_bang_honor_default_proc

Make slice! honor default hash value/proc
  • Loading branch information...
2 parents 6ac677f + f13fce6 commit 074c155bf60fa4b68db8b7253e1fa8077b4f2fe7 @rafaelfranca rafaelfranca committed Oct 24, 2013
@@ -1,3 +1,7 @@
+* Fix `slice!` deleting the default value of the hash.
+
+ *Antonio Santos*
+
* `require_dependency` accepts objects that respond to `to_path`, in
particular `Pathname` instances.
@@ -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
@@ -781,6 +781,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 074c155

Please sign in to comment.