Today I Learned

hashrocket A Hashrocket project

Compact and blank? in one method

I've "always" known about Ruby's compact method to get rid of nil values.

# Enumerable.compact
[1, "a", nil, " ", [], {}].compact
=> [1, "a", " ", [], {}]

However, I just learned that as of Rails 6.1 there has been a compact_blank method to get rid of those items that would respond true to blank?

# Enumerable.compact_blank
[1, "a", nil, " ", [], {}].compact_blank
=> [1, "a"]
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.