diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-03-08 09:54:34 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-03-08 09:54:34 -0800 |
commit | 085cb3b9af9d74fa9a3b72181f674f0b7c3dbf01 (patch) | |
tree | f6047abb60991991eb7c668397f6d012f89487cd /activesupport | |
parent | 58a55595ca0f23445488b590bf29890244fb2675 (diff) | |
parent | 74cb4b63bdbf34202f77ba17bcd15374a4900b03 (diff) | |
download | rails-085cb3b9af9d74fa9a3b72181f674f0b7c3dbf01.tar.gz rails-085cb3b9af9d74fa9a3b72181f674f0b7c3dbf01.tar.bz2 rails-085cb3b9af9d74fa9a3b72181f674f0b7c3dbf01.zip |
Merge pull request #5287 from nashby/hash-slice-extract
refactor Hash#slice and Hash#extract!
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/slice.rb | 12 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 7 |
2 files changed, 11 insertions, 8 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index 0484d8e5d8..fbc6e538d5 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -13,17 +13,15 @@ class Hash # valid_keys = [:mass, :velocity, :time] # search(options.slice(*valid_keys)) def slice(*keys) - keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) - hash = self.class.new - keys.each { |k| hash[k] = self[k] if has_key?(k) } - hash + keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) + keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) } end # Replaces the hash with only the given keys. # Returns a hash contained the removed key/value pairs # {:a => 1, :b => 2, :c => 3, :d => 4}.slice!(:a, :b) # => {:c => 3, :d => 4} def slice!(*keys) - keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) + keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) omit = slice(*self.keys - keys) hash = slice(*keys) replace(hash) @@ -33,8 +31,6 @@ class Hash # Removes and returns the key/value pairs matching the given keys. # {:a => 1, :b => 2, :c => 3, :d => 4}.extract!(:a, :b) # => {:a => 1, :b => 2} def extract!(*keys) - result = {} - keys.each {|key| result[key] = delete(key) } - result + keys.each_with_object({}) {|key, result| result[key] = delete(key) } end end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 38cdda6c5c..d71e3f968f 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -486,6 +486,13 @@ class HashExtTest < ActiveSupport::TestCase assert_equal 'bender', slice['login'] end + def test_extract + original = {:a => 1, :b => 2, :c => 3, :d => 4} + expected = {:a => 1, :b => 2} + + assert_equal expected, {:a => 1, :b => 2, :c => 3, :d => 4}.extract!(:a, :b) + end + def test_except original = { :a => 'x', :b => 'y', :c => 10 } expected = { :a => 'x', :b => 'y' } |