Assign to an array and replace emerged nil values

后端 未结 6 1026
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-17 19:08

Greetings!

When assigning a value to an array as in the following, how could I replace the nils by 0?

array = [1,2,3]
array         


        
6条回答
  •  被撕碎了的回忆
    2021-01-17 19:21

    nil.to_i is 0, if all the numbers are integers then below should work. I think It is also the shortest answer here.

    array.map!(&:to_i)
    

提交回复
热议问题