提交 074c155b 编写于 作者: R Rafael Mendonça França

Merge pull request #12611 from antonio/slice_bang_honor_default_proc

Make slice! honor default hash value/proc
* 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}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册