public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Faster Hash#slice that doesn't use Enumerable#include?.

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
chuyeow (author)
Mon Jun 02 01:42:38 -0700 2008
jeremy (committer)
Mon Jun 02 11:47:15 -0700 2008
commit  952ec79bec313e0001adfc8c86f7970448d32db9
tree    52d2ce3c0f14232e76ce5267bfe1a62435591f87
parent  714d42d1a6140ac4f6fc478bf1766d2b0aedb251
...
15
16
17
18
 
 
 
19
20
21
...
15
16
17
 
18
19
20
21
22
23
0
@@ -15,7 +15,9 @@ module ActiveSupport #:nodoc:
0
         # Returns a new hash with only the given keys.
0
         def slice(*keys)
0
           allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
0
-          reject { |key,| !allowed.include?(key) }
0
+          hash = {}
0
+          allowed.each { |k| hash[k] = self[k] }
0
+          hash
0
         end
0
 
0
         # Replaces the hash with only the given keys.

Comments

denis Mon Jun 02 14:51:40 -0700 2008

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 Mon Jun 02 14:57:51 -0700 2008

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

djd Mon Jun 02 16:04:40 -0700 2008

Why not use the returning(...) syntax?

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

josh Mon Jun 02 16:31:23 -0700 2008

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

djd Mon Jun 02 16:53:50 -0700 2008

Fair enough, thanks for the clarification.