On a related note, both solution create one Proc (in the block=>Proc
conversion done by &). The only difference is in the number of created
Array objects when the arity is 2 (two by hash elements for the facet
solution, one by element for the new implementation).
test code
=================================
require 'pp'
require 'utilrb/objectstats'
class Hash
if ARGV.empty?
STDERR.puts "using facet implementation"
# File lib/facets/core/hash/each.rb, line 19
def each(&yld)
case yld.arity
when 0
when 1
each_value{|v| yield(v)}
else
each_pair{|k,v| yld.call(k,v)}
end
self
end
else
STDERR.puts "using new implementation"
def each(&block)
if block.arity < 2
each_value(&block)
else
each_pair(&block)
end
end
end
end
test = { :a => 1, :b => 2, :c => 3 }
pp ObjectStats.profile { test.each { |a| } }
pp ObjectStats.profile { test.each { |a, b| } }
=================================
objectstats.rb is available here
http://www.laas.fr/~sjoyeux/darcs/utilrb/lib/utilrb/objectstats.rb
--
Sylvain Joyeux