diff options
author | Xavier Noria <fxn@hashref.com> | 2009-09-12 14:44:53 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2009-09-12 14:44:53 +0200 |
commit | f56b814671ba686f4bfc5b1a7ca5a32d11a33846 (patch) | |
tree | 6d5f14b8f720282d6ceeae468971352f6addd4e6 /railties/guides/source/active_support_overview.textile | |
parent | 848bdaaa222603c9efefc7b7a22e35ddc25a0f0f (diff) | |
download | rails-f56b814671ba686f4bfc5b1a7ca5a32d11a33846.tar.gz rails-f56b814671ba686f4bfc5b1a7ca5a32d11a33846.tar.bz2 rails-f56b814671ba686f4bfc5b1a7ca5a32d11a33846.zip |
AS guide: documents Hash#except
Diffstat (limited to 'railties/guides/source/active_support_overview.textile')
-rw-r--r-- | railties/guides/source/active_support_overview.textile | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index 5723fbffa2..392640d239 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/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+ |