首页 > 其他 > 详细

ruby 中each, map和collect的区别

时间:2015-05-22 14:49:29      阅读:205      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!