aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Martinez <sebastian@wyeworks.com>2011-08-15 20:20:28 -0300
committerSebastian Martinez <sebastian@wyeworks.com>2011-08-15 20:20:28 -0300
commit308595739c32609ac5b167ecdc6cb6cb4ec6c81f (patch)
treeae53554d3aae8eaced3a28e3986cad13b5435067
parent583d7c15c301f15f1e997a06e15b55a2e7bf794f (diff)
downloadrails-308595739c32609ac5b167ecdc6cb6cb4ec6c81f.tar.gz
rails-308595739c32609ac5b167ecdc6cb6cb4ec6c81f.tar.bz2
rails-308595739c32609ac5b167ecdc6cb6cb4ec6c81f.zip
Document Hash#extract!.
-rw-r--r--activesupport/lib/active_support/core_ext/hash/slice.rb2
-rw-r--r--railties/guides/source/active_support_core_extensions.textile12
2 files changed, 14 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb
index d7fb2da0fb..0484d8e5d8 100644
--- a/activesupport/lib/active_support/core_ext/hash/slice.rb
+++ b/activesupport/lib/active_support/core_ext/hash/slice.rb
@@ -30,6 +30,8 @@ class Hash
omit
end
+ # 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) }
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile
index 781d3d08cd..a672b17e4a 100644
--- a/railties/guides/source/active_support_core_extensions.textile
+++ b/railties/guides/source/active_support_core_extensions.textile
@@ -2696,6 +2696,18 @@ hash # => {:a => 1}
NOTE: Defined in +active_support/core_ext/hash/slice.rb+.
+h4. Extracting
+
+The method +extract!+ removes and returns the key/value pairs matching the given keys.
+
+<ruby>
+hash = {:a => 1, :b => 2}
+rest = hash.extract!(:a) # => {:a => 1}
+hash # => {:b => 2}
+</ruby>
+
+NOTE: Defined in +active_support/core_ext/hash/slice.rb+.
+
h4. Indifferent Access
The method +with_indifferent_access+ returns an +ActiveSupport::HashWithIndifferentAccess+ out of its receiver: