Rails 6.1 adds compact_blank

--

Rails 6.1.0 adds compact_blank and compact_blank! to ActiveSupport which makes it easier for removing blank values from an Enumerable / ActionController::Parameters.

Before Rails 6.1

Removing blank values from an array:

Removing blank values from a hash:

Rails 6.1

Removing blank values from an array:

You can also use compact_blank! which mutates its receiver:

Removing blank values from a hash:

Using compact_blank!:

Check out the pull request to see the implementation.

--

--