【问题标题】:What is the difference between map and collect in ruby [duplicate]ruby中的map和collect有什么区别[重复]
【发布时间】:2016-12-19 02:56:55
【问题描述】:
puts "Example of each"
  x = [1,2,3]
  a = x.each{ |i|
    i+1
  }

  puts a.inspect
  puts x.inspect

puts "Example of map"
  b = x.map{ |i|
    i+1
  }

  puts b.inspect
  puts x.inspect

puts "Example of collect"
  c = x.collect{  |i|
    i+1
  }

  puts c.inspect
  puts x.inspect  

输出

Example of each
[1, 2, 3]
[1, 2, 3]
Example of map
[2, 3, 4]
[1, 2, 3]
Example of collect
[2, 3, 4]
[1, 2, 3]

在这里,我们看到每个块返回相同的值传递给它,而不管它内部的操作。而且地图和收集似乎是一样的。那么基本上map和collect有什么区别呢?

【问题讨论】:

标签: ruby


【解决方案1】:

绝对没有,它是一个别名。

【讨论】:

    猜你喜欢
    • 2012-03-14
    • 2013-03-28
    • 2016-12-24
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多