Ruby 中的each 和collect 方法有什么不同
从这段代码中我不知道 collect
和 each
这两种方法之间的区别。
a = ["L","Z","J"].collect{|x| puts x.succ} #=> M AA K
print a.class #=> Array
b = ["L","Z","J"].each{|x| puts x.succ} #=> M AA K
print b.class #=> Array
From this code I don't know the difference between the two methods, collect
and each
.
a = ["L","Z","J"].collect{|x| puts x.succ} #=> M AA K
print a.class #=> Array
b = ["L","Z","J"].each{|x| puts x.succ} #=> M AA K
print b.class #=> Array
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Array#each 接受一个数组并将给定的块应用于所有项目。它不会影响数组或创建新对象。这只是循环项目的一种方式。它还返回自身。
打印 2,4,6,8 并返回 [1,2,3,4],无论
Array#collect 与 Array#map 相同并且应用给定的所有项目上的代码块并返回新数组。只需将 '将序列的每个元素投影为新形式'
返回 [2,4,6,8]
并且在您的代码中
a 是一个数组,但它实际上是 Nil 的 数组[nil,nil,nil] 因为
puts x.succ
返回nil
(即使它打印 M AAK)。并且
也是一个数组。但它的值是["L","Z","J"],因为它返回self。
Array#each
takes an array and applies the given block over all items. It doesn't affect the array or creates a new object. It is just a way of looping over items. Also it returns self.Prints 2,4,6,8 and returns [1,2,3,4] no matter what
Array#collect
is same asArray#map
and it applies the given block of code on all the items and returns the new array. simply put 'Projects each element of a sequence into a new form'Returns [2,4,6,8]
And In your code
a is an Array but it is actually an array of Nil's [nil,nil,nil] because
puts x.succ
returnsnil
(even though it prints M AA K).And
also is an Array. But its value is ["L","Z","J"], because it returns self.
Array#each 只是获取每个元素并将其放入块中,然后返回原始数组。 Array#collect 获取每个元素并将其放入返回的新数组中:
Array#each
just takes each element and puts it into the block, then returns the original array.Array#collect
takes each element and puts it into a new array that gets returned:each
适用于当您想要迭代数组并在每次迭代中执行您想要的操作时。在大多数(命令式)语言中,这是程序员在需要处理列表时使用的“一刀切”锤子。对于更多的函数式语言,如果您无法以任何其他方式执行此操作,则只能执行此类通用迭代。大多数时候,map或reduce会更合适(在ruby中收集和注入)
collect
适用于当你想将一个数组转换为另一个数组时inject
适用于当你想将数组转换为单个值时each
is for when you want to iterate over an array, and do whatever you want in each iteration. In most (imperative) languages, this is the "one size fits all" hammer that programmers reach for when you need to process a list.For more functional languages, you only do this sort of generic iteration if you can't do it any other way. Most of the time, either map or reduce will be more appropriate (collect and inject in ruby)
collect
is for when you want to turn one array into another arrayinject
is for when you want to turn an array into a single value根据 docs,这是两个源代码片段...
rb_yield()
返回块返回的值 (另请参阅这篇关于元编程的博客文章)。因此,
each
只是yields 并返回原始数组,而collect
创建一个新数组并将块的结果推入其中;然后它返回这个新数组。源代码片段:每个,收集
Here are the two source code snippets, according to the docs...
rb_yield()
returns the value returned by the block (see also this blog post on metaprogramming).So
each
just yields and returns the original array, whilecollect
creates a new array and pushes the results of the block into it; then it returns this new array.Source snippets: each, collect
区别在于它返回的内容。在你上面的例子中
a == [nil,nil,nil]
(puts x.succ 的值)而b == ["L", "Z", "J"]
(原始数组)根据 ruby-doc,collect 执行以下操作:
每个总是返回原始数组。有道理吗?
The difference is what it returns. In your example above
a == [nil,nil,nil]
(the value of puts x.succ) whileb == ["L", "Z", "J"]
(the original array)From the ruby-doc, collect does the following:
Each always returns the original array. Makes sense?
每个都是包含 Enumerable 模块的所有类定义的方法。
Object.each
返回一个Enumerable::Enumerator
对象。这就是其他 Enumerable 方法用来迭代对象的方法。每个类的each
方法的行为不同。在 Array 类中,当一个块传递给
each
时,它会对每个元素执行该块的语句,但最终返回 self。当您不需要数组时,这很有用,但您可能只是想从数组中选择元素并将其用作其他方法的参数。inspect
和map
返回一个新数组,其中包含每个元素上块执行的返回值。您可以使用map!
和collect!
对原始数组进行操作。Each is a method defined by all classes that include the Enumerable module.
Object.each
returns aEnumerable::Enumerator
Object. This is what other Enumerable methods use to iterate through the object.each
methods of each class behaves differently.In Array class when a block is passed to
each
, it performs statements of the block on each element, but in the end returns self.This is useful when you don't need an array, but you maybe just want to choose elements from the array and use the as arguments to other methods.inspect
andmap
return a new array with return values of execution of the block on each element. You can usemap!
andcollect!
to perform operations on the original array.我认为更简单的理解方法如下:
相反,如果您使用collect:
并且另外,您可以使用
.collect!
来改变原始数组。I think an easier way to understand it would be as below:
Instead, if you use collect:
And plus, you can use
.collect!
to mutate the original array.