mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Parameterize A String With Underscores as a Rails TIL
This commit is contained in:
40
rails/parameterize-a-string-with-underscores.md
Normal file
40
rails/parameterize-a-string-with-underscores.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# Parameterize A String With Underscores
|
||||
|
||||
I have human-readable status strings that I'm working with like `In progress`,
|
||||
`Pending approval`, and `Completed`. I need to deterministically turn those
|
||||
into parameterized values that I can compare. That is, I want them lowercased
|
||||
and separated by underscores instead of spaces.
|
||||
|
||||
The `ActiveSupport` `#parameterize` method, as is, gets me pretty close.
|
||||
|
||||
```ruby
|
||||
> statuses = [
|
||||
"In progress",
|
||||
"Pending approval",
|
||||
"Completed"
|
||||
]
|
||||
|
||||
> statuses.map(&:parameterize)
|
||||
=> [
|
||||
"in-progress",
|
||||
"pending-approval",
|
||||
"completed"
|
||||
]
|
||||
```
|
||||
|
||||
Those are separated by dashes though. Fortunately, `parameterize` takes a
|
||||
`separator` option that we can use to verride what character is used to
|
||||
separate words. Let's use an underscore (`_`).
|
||||
|
||||
```ruby
|
||||
> statuses.map { |str| str.parameterize(separator: '_') }
|
||||
=> [
|
||||
"in_progress",
|
||||
"pending_approval",
|
||||
"completed"
|
||||
]
|
||||
```
|
||||
|
||||
See the [`#paramterize`
|
||||
docs](https://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize)
|
||||
for more details.
|
||||
Reference in New Issue
Block a user