Skip to content

Commit

Permalink
Merge pull request #12611 from antonio/slice_bang_honor_default_proc
Browse files Browse the repository at this point in the history
Make slice! honor default hash value/proc
Conflicts:
	activesupport/CHANGELOG.md
  • Loading branch information
rafaelfranca committed Oct 24, 2013
1 parent 961f00a commit e5e305e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,5 +1,9 @@
## unreleased ## ## unreleased ##


* Fix `slice!` deleting the default value of the hash.

*Antonio Santos*

* Disable the ability to iterate over Range of AS::TimeWithZone * Disable the ability to iterate over Range of AS::TimeWithZone
due to significant performance issues. due to significant performance issues.


Expand Down
2 changes: 2 additions & 0 deletions activesupport/lib/active_support/core_ext/hash/slice.rb
Expand Up @@ -26,6 +26,8 @@ def slice!(*keys)
keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
omit = slice(*self.keys - keys) omit = slice(*self.keys - keys)
hash = slice(*keys) hash = slice(*keys)
hash.default = default
hash.default_proc = default_proc if default_proc
replace(hash) replace(hash)
omit omit
end end
Expand Down
18 changes: 18 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Expand Up @@ -751,6 +751,24 @@ def test_indifferent_slice_access_with_symbols
assert_equal 'bender', slice['login'] assert_equal 'bender', slice['login']
end 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 def test_extract
original = {:a => 1, :b => 2, :c => 3, :d => 4} original = {:a => 1, :b => 2, :c => 3, :d => 4}
expected = {:a => 1, :b => 2} expected = {:a => 1, :b => 2}
Expand Down

0 comments on commit e5e305e

Please sign in to comment.