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

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
Cheah Chu Yeow
2008-06-02 01:42:38 -07:00
committed by Jeremy Kemper
parent 714d42d1a6
commit 952ec79bec

View File

@@ -15,7 +15,9 @@ module ActiveSupport #:nodoc:
# 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.