2014年3月25日 星期二

ruby , each vs map vs collect

來源 http://stackoverflow.com/questions/9429034/what-is-the-difference-between-map-each-and-collect

each is different from map and collect , but map and collect are the same

each performs the enclosed block for each element in the receiver
[1,2,3,4].each {|n| puts n*2}
# Outputs:
# 2
# 4
# 6
# 8
map and collect produce a new Array containing the results of the block applied to each ekenebt if the receiver
[1,2,3,4].map {|n| n*2}
# => [2,4,6,8]

沒有留言:

張貼留言