Learn Ruby

compact to remove all nil elements

If an array has nil items and we want to remove nil items then we can use compact method.

As we can see array2 does not have any nil items. Note that compact does not change the original array. The original array1 is still containing nil items. compact method returns a new array which has nil items removed.