array.each 和 array.map 有什么不同?
可能的重复:
Array#each 与 Array#map
ruby-1.9.2-p180 :006 > ary = ["a", "b"]
=> ["a", "b"]
ruby-1.9.2-p180 :007 > ary.map { |val| p val }
"a"
"b"
=> ["a", "b"]
ruby-1.9.2-p180 :008 > ary.each { |val| p val }
"a"
"b"
=> ["a", "b"]
ruby-1.9.2-p180 :009 > ary.map { |val| val << "2" }
=> ["a2", "b2"]
ruby-1.9.2-p180 :010 > ary.each { |val| val << "2" }
=> ["a22", "b22"]
Possible Duplicate:
Array#each vs. Array#map
ruby-1.9.2-p180 :006 > ary = ["a", "b"]
=> ["a", "b"]
ruby-1.9.2-p180 :007 > ary.map { |val| p val }
"a"
"b"
=> ["a", "b"]
ruby-1.9.2-p180 :008 > ary.each { |val| p val }
"a"
"b"
=> ["a", "b"]
ruby-1.9.2-p180 :009 > ary.map { |val| val << "2" }
=> ["a2", "b2"]
ruby-1.9.2-p180 :010 > ary.each { |val| val << "2" }
=> ["a22", "b22"]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
副作用是相同的,这会给你的逆向工程带来一些混乱。
是的,两者都迭代数组(实际上,混合在Enumerable),但map将返回一个由块结果组成的数组,而each将仅返回原始数组。 each 的返回值很少在 Ruby 代码中使用,但 map 是 最重要的功能工具。
顺便说一句,您可能很难找到文档,因为 map 是 Enumerable 中的一种方法,而 each (enumerable 所需的一个方法) em>Enumerable 模块)是 Array 中的一个方法。
需要注意的是:map 实现基于 each。
The side effects are the same which is adding some confusion to your reverse engineering.
Yes, both iterate over the array (actually, anything that mixes in Enumerable) but map will return an Array composed of the block results while each will just return the original Array. The return value of each is rarely used in Ruby code but map is one of the most important functional tools.
BTW, you may be having a hard time finding the documentation because map is a method in Enumerable while each (the one method required by the Enumerable module) is a method in Array.
As a trivia note: the map implementation is based on each.
API 文档中的定义:
each:为 self 中的每个元素调用一次 block,将该元素作为参数传递。
map:为 self 的每个元素调用一次 block。创建一个包含块返回的值的新数组。
因此,每个都是一个正常的循环,它迭代每个元素并调用给定的块
映射,通常用于您希望通过某种逻辑将另一个数组映射到现有数组的情况。您还可以将方法作为对映射函数的引用传递,例如
definition from API docs:
each: Calls block once for each element in self, passing that element as a parameter.
map: invokes block once for each element of self. Creates a new array containing the values returned by the block.
so each is a normal loop, which iterates through each element and invokes given block
map generally used where you want another array mapped by some logic to existing one. You can also pass method as reference to map function like
Array#map 是每个元素在阻塞中返回的任何内容的集合。
Array#each 为每个元素执行代码块,然后返回列表本身。
您应该检查这个Array#each 与 Array#map
Array#map is a collection of whatever is returned in the blocked for each element.
Array#each execute the block of code for each element, then returns the list itself.
You should check this Array#each vs. Array#map
每个只是一个迭代器,它获取可迭代的下一个元素并馈送到块。 Map 用于迭代,但将元素映射到其他内容,例如值乘以常量。在您的示例中,它们可以互换使用,但每个都是一个更通用的迭代器,它将元素传递到块。
Each is just an iterator that gets the next element of an iterable and feeds to a block. Map is used to iterate but map the elements to something else, like a value multiplied by a constant. In your examples, they can be used interchangeably, but each is a more generic iterator that passes the element to a block.