Permalink
Browse files

AS guide: documents Hash#except

  • Loading branch information...
fxn committed Sep 12, 2009
1 parent 848bdaa commit f56b814671ba686f4bfc5b1a7ca5a32d11a33846
Showing with 23 additions and 0 deletions.
  1. +23 −0 railties/guides/source/active_support_overview.textile
@@ -1166,6 +1166,29 @@ hash1.diff(hash2).diff(hash2) == hash1
Diffing hashes may be useful for error messages related to expected option hashes for example.
+h4. Removing Keys
+
+The method +except+ returns a hash with the keys in the argument list removed, if present:
+
+<ruby>
+{:a => 1, :b => 2}.except(:a) # => {:b => 2}
+</ruby>
+
+If the receiver responds to +convert_key+, the method is called on each of the arguments. This allows +except+ to play nice with hashes with indifferent access for instance:
+
+<ruby>
+{:a => 1}.with_indifferent_access.except(:a) # => {}
+{:a => 1}.with_indifferent_access.except("a") # => {}
+</ruby>
+
+The method +except+ may come in handy for example when you want to protect some parameter that can't be globally protected with +attr_protected+:
+
+<ruby>
+params[:account] = params[:account].except(:plan_id) unless admin?
+@account.update_attributes(params[:account])
+</ruby>
+
+There's also the bang variant +except!+ that removes keys in the very receiver.
h3. Extensions to +Range+

0 comments on commit f56b814

Please sign in to comment.