Assign to an array and replace emerged nil values

后端 未结 6 1130
佛祖请我去吃肉
佛祖请我去吃肉 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:37

    There is no built-in function to replace nil in an array, so yes, map is the way to go. If a shorter version would make you happier, you could do:

    array.map {|e| e ? e : 0}
    

提交回复
热议问题