0027 Compact

Filter nil values

The Enumerable module defines the compact method, which returns an Array containing all non-nil elements. The Hash overloads the method and returns a Hash containing all entries whose values are not nil.

0027-compact_1.png

In addition, Array and Hash define compact! which modifies the instance. ⚠️ The method returns nil if there is no modification.

0027-compact_2.png

Links to Ruby documentation for Array#compact and Hash#compact.