ruby 1.8.7 to_proc creates empty arrays

我与影子孤独终老i 提交于 2019-12-07 15:33:23

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!