ruby 注入与减少红宝石相同吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13813243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:35:21  来源:igfitidea点击:

Is inject the same thing as reduce in ruby?

rubyaliasreduceinject

提问by Hymany

I saw that they were documented together here. Are they the same thing? Why does Ruby have so many aliases (such as map/collect for arrays)? Thanks a lot.

我看到它们被一起记录在这里。它们是一样的吗?为什么 Ruby 有这么多别名(比如数组的 map/collect)?非常感谢。

回答by Zach

Yes, and it's also called foldin 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 #lengthon an Array, you can. If you want to use #size, that's fine too!

是的,它fold在许多其他编程语言和数学中也被调用。Ruby 别名很多,以便对不同背景的程序员直观。如果你想在#length上使用Array,你可以。如果你想使用#size,那也没关系!

回答by axiac

More recent versions of the documentation of Enumerable#reducespecify it explicitly:

文档的更新版本Enumerable#reduce明确指定它:

The injectand reducemethods are aliases. There is no performance benefit to either.

injectreduce方法是别名。两者都没有性能优势。