Skip to content

Commit

Permalink
AS guide: documents Hash#except
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Sep 12, 2009
1 parent 848bdaa commit f56b814
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions railties/guides/source/active_support_overview.textile
Expand Up @@ -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. 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+ h3. Extensions to +Range+


Expand Down

0 comments on commit f56b814

Please sign in to comment.