Compact an array: it removes the nils : nil « Language Basics « Ruby






Compact an array: it removes the nils


a = [1, 2, nil, 3, 3, nil, nil, nil, 5]
p a.compact                                  # => [1, 2, 3, 3, 5]

p a.delete(3)
a                                          # => [1, 2, nil, nil, nil, nil, 5]

 








Related examples in the same category

1.Assign nil to array element
2.Is it a nil value
3.If x is non-nil
4.see if it is nil: