mirror of
https://github.com/jbranchaud/til
synced 2026-09-01 01:01:46 +00:00
Add Remove Blank Values From A Hash as a Rails TIL
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
```
|
||||
Reference in New Issue
Block a user