Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Hash#except which is the inverse of Hash#slice -- return the ha…
…sh except the keys that are specified [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7172 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
Showing
4 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'set' | ||
|
||
module ActiveSupport #:nodoc: | ||
module CoreExtensions #:nodoc: | ||
module Hash #:nodoc: | ||
# Return a hash that includes everything but the given keys. This is useful for | ||
# limiting a set of parameters to everything but a few known toggles: | ||
# | ||
# @person.update_attributes(params[:person].except(:admin)) | ||
module Except | ||
# Returns a new hash without the given keys. | ||
def except(*keys) | ||
rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) | ||
reject { |key,| rejected.include?(key) } | ||
end | ||
|
||
# Replaces the hash without only the given keys. | ||
def except!(*keys) | ||
replace(except(*keys)) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters