问题
This is a follow-up to to this answer, regarding ruby 1.8.7's Symbol#to_proc generating a new proc every invocation.
There seems to be more going on than the answers suggest.
Here's some sample code:
def ctob
h=Hash.new(0)
ObjectSpace.each_object(Object) {|e| h[e.class]+=1 }
h
end
r=(0...1000)
p ctob
r.map(&:to_i)
p ctob
This reveals that about a thousand arrays are being created. This reveals that about a thousand are empty:
c=0; ObjectSpace.each_object(Array){|e| c+=1 if e.empty? }
Another interesting thing is that only one Proc object exists. This suggests that to_proc
is only called once. (Maybe another one would get created if I called map
with a symbol a second time.)
If I change the map call to use a block, these arrays are not created. This also might explain why Andrew Grimm's caching didn't help the benchmarks. Why are these arrays being created?
UPDATE
Apparently a proc created from a Symbol creates an empty array every time it's called.
If I replace the map
line above with
pr=:to_i.to_proc; r.map(&pr)
causes the arrays to be created, but this
pr=proc{|e|e.to_i}; r.map(&pr)
does not. Similar thing happens if I just do pr.call(value).
(When is a proc not a proc?)
回答1:
I think I found the answer.
I looked at activesupport 2.2 and found this as the body of Symbol#to_proc
:
Proc.new { |*args| args.shift.__send__(self, *args) }
args
is the array. Since each member of the range is passed as a single arg, it gets converted to an array of 1 element. That one element is shifted off, leaving an empty array. So it's not creating empty arrays, it's just leaving them behind after processing the args.
I've also done a test using a 2-arg proc:
[1,2,3,4].inject(&:+)
This leaves behind arrays of 1 element (the original first element is the current sum).
My assumption is that 1.8.7 does something similar. I'm curious to know how 1.9 does it differently.
来源:https://stackoverflow.com/questions/11781800/ruby-1-8-7-to-proc-creates-empty-arrays