diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-04-02 11:45:03 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-04-02 11:45:03 +0000 |
commit | adaed1ebcb4af2a7203d61e70b826f69f880fb2f (patch) | |
tree | 50e4b4299b71241e46a24ce36b0e8a66d67ba277 /activesupport/lib/active_support/core_ext | |
parent | f4dc83497612bbd458213fccd161e1a38939ad5f (diff) | |
download | rails-adaed1ebcb4af2a7203d61e70b826f69f880fb2f.tar.gz rails-adaed1ebcb4af2a7203d61e70b826f69f880fb2f.tar.bz2 rails-adaed1ebcb4af2a7203d61e70b826f69f880fb2f.zip |
Adding Hash#without Closes #7369 [eventualbuddha]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9209 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/slice.rb | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index 6fe5e05330..c270e3906b 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -11,6 +11,11 @@ module ActiveSupport #:nodoc: # end # # search(options.slice(:mass, :velocity, :time)) + # + # Also allows leaving out certain keys. This is useful when duplicating + # a hash but omitting a certain subset: + # + # Event.new(event.attributes.without(:id, :user_id)) module Slice # Returns a new hash with only the given keys. def slice(*keys) @@ -22,6 +27,17 @@ module ActiveSupport #:nodoc: def slice!(*keys) replace(slice(*keys)) end + + # Returns a new hash without the given keys. + def without(*keys) + allowed = self.keys - (respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) + slice(*allowed) + end + + # Replaces the hash without the given keys. + def without!(*keys) + replace(without(*keys)) + end end end end |