Is inject the same thing as reduce in ruby?

冷暖自知 提交于 2019-11-28 18:30:19

Yes, and it's also called fold in many other programming languages and in Mathematics. Ruby aliases a lot in order to be intuitive to programmers with different backgrounds. If you want to use #length on an Array, you can. If you want to use #size, that's fine too!

More recent versions of the documentation of Enumerable#reduce specify it explicitly:

The inject and reduce methods are aliases. There is no performance benefit to either.

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