arr = [1,2,3]

1) arr2 = arr.each{|element| element = element * 2} #arr与arr2仍然都等于[1,2,3]   each返回原数组 遍历内对元素的更改不会保存
2) arr2 = arr.map{|element| element = element* 2} #arr等于[1,2,3] arr2等于[2,4,6] map返回更改后的数组 遍历内对元素的更改不会保存
3) arr2 = arr.map!{|element| element = element * 2} #arr与arr2都等于[2,4,6] map!返回更改后的数组 遍历对元素内的更改会保存

collect 效果等于 map
collect! 效果等于map!

相关文章:

  • 2021-06-02
  • 2022-01-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-16
  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案