Skip to content
This repository
Browse code

Faster Hash#slice that doesn't use Enumerable#include?.

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit 952ec79bec313e0001adfc8c86f7970448d32db9 1 parent 714d42d
Cheah Chu Yeow chuyeow authored jeremy committed
4 activesupport/lib/active_support/core_ext/hash/slice.rb
@@ -15,7 +15,9 @@ module Slice
15 15 # Returns a new hash with only the given keys.
16 16 def slice(*keys)
17 17 allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
18   - reject { |key,| !allowed.include?(key) }
  18 + hash = {}
  19 + allowed.each { |k| hash[k] = self[k] }
  20 + hash
19 21 end
20 22
21 23 # Replaces the hash with only the given keys.

6 comments on commit 952ec79

Denis Barushev
denis commented on 952ec79

This commit has broke form helpers! Now they generate something like name=“list[][name]” instead of name=“list[name]”. All forms in my app are broken.

Denis Barushev
denis commented on 952ec79

denis@denis-laptop:~/dev/ruby/rails/actionpack$ rake 1966 tests, 9460 assertions, 16 failures, 0 errors

Lourens Naudé

see http://github.com/methodmissing/rails/commit/fee25cfcb6c8a29ff1906d54ba849349f38ebedd for the fix

DJ Davis
djd commented on 952ec79

Why not use the returning(…) syntax?

returning {} do |allowed| allowed.each { |k| hash[k] = self[k] } end

Joshua Peek
Collaborator
josh commented on 952ec79

We get shit for that (performance). Looks much nicer but I guess you don’t need it for really low level stuff like this.

DJ Davis
djd commented on 952ec79

Fair enough, thanks for the clarification.

Please sign in to comment.
Something went wrong with that request. Please try again.