码迷,mamicode.com
首页 > 其他好文 > 详细

ruby 中each, map和collect的区别

时间:2015-05-22 14:52:10      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

map and collect are the same, they return an array constructed as the result of calling the block for each item in the array.

irb(main):002:0> [1,2,3,4].collect {|n| n*2}
=> [2, 4, 6, 8]
irb(main):003:0> [1,2,3,4].map {|n| n*2}
=> [2, 4, 6, 8]

each will evaluate the block but throws away the result of each block‘s evaluation and returns the original array.

irb(main):001:0> [1,2,3,4].each {|n| n*2}
=> [1, 2, 3, 4]

  

 

ruby 中each, map和collect的区别

标签:

原文地址:http://www.cnblogs.com/lucien06/p/4522119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!