Can't all or most cases of `each` be replaced with `map`?

后端 未结 4 1561
情歌与酒
情歌与酒 2021-01-13 11:11

The difference between Enumerable#each and Enumerable#map is whether it returns the receiver or the mapped result. Getting back to the receiver is

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-13 11:24

    The choice between map or each should be decided by the desired end result: a new array or no new array. The result of map can be huge and/or silly:

    p ("aaaa".."zzzz").map{|word| puts word} #huge and useless array of nil's
    

提交回复
热议问题