aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorVasiliy Ermolovich <younash@gmail.com>2012-03-05 21:35:57 +0300
committerVasiliy Ermolovich <younash@gmail.com>2012-03-06 08:33:14 +0300
commit74cb4b63bdbf34202f77ba17bcd15374a4900b03 (patch)
tree820664f01b23863b82bf1c6077f19911c020c2f7 /activesupport/lib
parentc5a47b3207de8db12cfe956fd75933b6930395b2 (diff)
downloadrails-74cb4b63bdbf34202f77ba17bcd15374a4900b03.tar.gz
rails-74cb4b63bdbf34202f77ba17bcd15374a4900b03.tar.bz2
rails-74cb4b63bdbf34202f77ba17bcd15374a4900b03.zip
refactor Hash#slice and Hash#extract!
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/slice.rb12
1 files changed, 4 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