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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store