From f56b814671ba686f4bfc5b1a7ca5a32d11a33846 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 12 Sep 2009 14:44:53 +0200 Subject: AS guide: documents Hash#except --- .../guides/source/active_support_overview.textile | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'railties') 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: + + +{:a => 1, :b => 2}.except(:a) # => {:b => 2} + + +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: + + +{:a => 1}.with_indifferent_access.except(:a) # => {} +{:a => 1}.with_indifferent_access.except("a") # => {} + + +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+: + + +params[:account] = params[:account].except(:plan_id) unless admin? +@account.update_attributes(params[:account]) + + +There's also the bang variant +except!+ that removes keys in the very receiver. h3. Extensions to +Range+ -- cgit v1.2.3