diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2019-04-02 17:28:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-02 17:28:50 -0400 |
commit | 736c7d5995265e5fe004658a10f6fe38a49a31bd (patch) | |
tree | cb943e8f279b614b8f3ab5c18ed4841bfbdb0ce6 | |
parent | d03177ffbccb5b30217448d526f1e9aa1fa24297 (diff) | |
parent | 49598c0638b8f8afeb10b20cf64ff83a2ea1d8b2 (diff) | |
download | rails-736c7d5995265e5fe004658a10f6fe38a49a31bd.tar.gz rails-736c7d5995265e5fe004658a10f6fe38a49a31bd.tar.bz2 rails-736c7d5995265e5fe004658a10f6fe38a49a31bd.zip |
Merge pull request #35771 from timoschilling/hash-speed-improvements
Hash / HashWithIndifferentAccess speed improvements
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/except.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/hash_with_indifferent_access.rb | 9 |
2 files changed, 7 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/except.rb b/activesupport/lib/active_support/core_ext/hash/except.rb index 6258610c98..5013812460 100644 --- a/activesupport/lib/active_support/core_ext/hash/except.rb +++ b/activesupport/lib/active_support/core_ext/hash/except.rb @@ -10,7 +10,7 @@ class Hash # This is useful for limiting a set of parameters to everything but a few known toggles: # @person.update(params[:person].except(:admin)) def except(*keys) - dup.except!(*keys) + slice(*self.keys - keys) end # Removes the given keys from hash and returns it. diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 3a2b2652c4..42ae7e9b7b 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -225,8 +225,8 @@ module ActiveSupport # hash[:a] = 'x' # hash[:b] = 'y' # hash.values_at('a', 'b') # => ["x", "y"] - def values_at(*indices) - indices.collect { |key| self[convert_key(key)] } + def values_at(*keys) + super(*keys.map { |key| convert_key(key) }) end # Returns an array of the values at the specified indices, but also @@ -239,7 +239,7 @@ module ActiveSupport # hash.fetch_values('a', 'c') { |key| 'z' } # => ["x", "z"] # hash.fetch_values('a', 'c') # => KeyError: key not found: "c" def fetch_values(*indices, &block) - indices.collect { |key| fetch(key, &block) } + super(*indices.map { |key| convert_key(key) }, &block) end # Returns a shallow copy of the hash. @@ -293,6 +293,9 @@ module ActiveSupport super(convert_key(key)) end + def except(*keys) + slice(*self.keys - keys.map { |key| convert_key(key) }) + end alias_method :without, :except def stringify_keys!; self end |