Permalink
Browse files

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

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
chuyeow authored and dhh committed Jun 2, 2008
1 parent 604a588 commit 7247ee6a49d32c27310121086cb3b6c6268c323a
Showing with 3 additions and 1 deletion.
  1. +3 −1 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.

0 comments on commit 7247ee6

Please sign in to comment.