From b484204c2f5887f37a5ff099932c766cdeea4c6e Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 6 Aug 2026 09:25:41 -0500 Subject: [PATCH] Add Remove Blank Values From A Hash as a Rails TIL --- README.md | 3 +- rails/remove-blank-values-from-a-hash.md | 51 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 rails/remove-blank-values-from-a-hash.md diff --git a/README.md b/README.md index 3b31fa3..dce0c37 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1851 TILs and counting..._ +_1852 TILs and counting..._ See some of the other learning resources I work on: @@ -1260,6 +1260,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Read-Only Models](rails/read-only-models.md) - [Rebuild Tailwind Bundle For Dev Server](rails/rebuild-tailwind-bundle-for-dev-server.md) - [Remove A Database Column From A Table](rails/remove-a-database-column-from-a-table.md) +- [Remove Blank Values From A Hash](rails/remove-blank-values-from-a-hash.md) - [Remove The Default Value On A Column](rails/remove-the-default-value-on-a-column.md) - [Render An Alternative ActionMailer Template](rails/render-an-alternative-action-mailer-template.md) - [Render The Response Body In Controller Specs](rails/render-the-response-body-in-controller-specs.md) diff --git a/rails/remove-blank-values-from-a-hash.md b/rails/remove-blank-values-from-a-hash.md new file mode 100644 index 0000000..53cb6b5 --- /dev/null +++ b/rails/remove-blank-values-from-a-hash.md @@ -0,0 +1,51 @@ +# Remove Blank Values From A Hash + +Ruby's [`Enumerable`](https://docs.ruby-lang.org/en/master/Enumerable.html) has +a method `#compact` that will remove `nil` values from `Enumerable` objects like +hashes and arrays. + +```ruby +> { one: nil, two: 2, three: "" }.compact +=> {two: 2, three: ""} +> [nil, 2, ""].compact +=> [2, ""] +``` + +That is often what I want because I'm thinking in terms of values being either +`nil` or valid value. + +Sometimes I want to remove all _blank_ values, not just the `nil` ones. This +happens in a Rails context like in a controller when dealing with parameters +from a request -- a form field was left blank (`""`) or no options were picked +from the multi-select (`[]`). + +Rails adds +[`#compact_blank`](https://api.rubyonrails.org/classes/Enumerable.html#method-i-compact_blank) +to `Enumerable` to support these cases. Consider that `#compact` corresponds to +`#nil?` where as `#compact_blank` corresponds to `#blank?`. + +Here is the example hash pulled from the docs: + +```ruby +> { a: "", b: 1, c: nil, d: [], e: false, f: true }.compact +=> {a: "", b: 1, d: [], e: false, f: true} +> { a: "", b: 1, c: nil, d: [], e: false, f: true }.compact_blank +=> {b: 1, f: true} +``` + +I might want to `compact_blank` a set of search parameters coming from the +client before passing it into my search service: + +```ruby +class BooksController < BaseController + def search + @results = SearchService.call(search_params.compact_blank) + end + + private + + def search_params + params.permit(...) + end +end +```