ruby 中each, map和collect的区别

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]

  

 

posted @ 2015-05-22 13:40  LUCIEN06  阅读(531)  评论(0编辑  收藏  举报