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]
时间: 2024-10-21 04:06:23