Skip to content
Browse files

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

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent 714d42d commit 952ec79bec313e0001adfc8c86f7970448d32db9 @chuyeow chuyeow committed with jeremy Jun 2, 2008
Showing with 3 additions and 1 deletion.
  1. +3 −1 activesupport/lib/active_support/core_ext/hash/slice.rb
View
4 activesupport/lib/active_support/core_ext/hash/slice.rb
@@ -15,7 +15,9 @@ module Slice
# Returns a new hash with only the given keys.
def slice(*keys)
allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
- reject { |key,| !allowed.include?(key) }
+ hash = {}
+ allowed.each { |k| hash[k] = self[k] }
+ hash
end
# Replaces the hash with only the given keys.

6 comments on commit 952ec79

@denis
denis commented on 952ec79 Jun 2, 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
denis commented on 952ec79 Jun 2, 2008

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

@methodmissing

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

@djd
djd commented on 952ec79 Jun 2, 2008

Why not use the returning(…) syntax?

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

@josh
Ruby on Rails member
josh commented on 952ec79 Jun 2, 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
djd commented on 952ec79 Jun 2, 2008

Fair enough, thanks for the clarification.

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