1
0
mirror of https://github.com/jbranchaud/til synced 2026-03-04 06:58:45 +00:00

Add Make A Long String Of Text Readable as a Ruby TIL

This commit is contained in:
jbranchaud
2026-02-12 17:30:15 -06:00
parent f186d5977d
commit ec12f7ea80
2 changed files with 40 additions and 1 deletions

View File

@@ -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). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
_1736 TILs and counting..._ _1737 TILs and counting..._
See some of the other learning resources I work on: See some of the other learning resources I work on:
@@ -1431,6 +1431,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Limit Split](ruby/limit-split.md) - [Limit Split](ruby/limit-split.md)
- [List The Running Ruby Version](ruby/list-the-running-ruby-version.md) - [List The Running Ruby Version](ruby/list-the-running-ruby-version.md)
- [Listing Local Variables](ruby/listing-local-variables.md) - [Listing Local Variables](ruby/listing-local-variables.md)
- [Make A Long String Of Text Readable](ruby/make-a-long-string-of-text-readable.md)
- [Make An Executable Ruby Script](ruby/make-an-executable-ruby-script.md) - [Make An Executable Ruby Script](ruby/make-an-executable-ruby-script.md)
- [Make Structs Easier To Use With Keyword Initialization](ruby/make-structs-easier-to-use-with-keyword-initialization.md) - [Make Structs Easier To Use With Keyword Initialization](ruby/make-structs-easier-to-use-with-keyword-initialization.md)
- [Map With Index Over An Array](ruby/map-with-index-over-an-array.md) - [Map With Index Over An Array](ruby/map-with-index-over-an-array.md)

View File

@@ -0,0 +1,38 @@
# Make A Long String Of Text Readable
I have a paragraph of text that interpolates a couple user-specific values
before being included in an API request. Because it is being passed to an API,
it is a single-line string value. However, in the editor it is hard to read like
that because it overflows way past the edge of the viewport.
```ruby
description = "This is the description we need to provide for #{user.name} as part of an API request dealing with compliance and registration for a service. If you need to contact them, their email is #{user.email}."
```
I'd rather make this easier on myself and others to read from the editor while
still being able to submit a single-line string to the API. That can be
accomplished with a heredoc and some combination or `gsub`, `strip`, and
`squish`.
If we are in a strictly Ruby-only context, we can use `gsub` and `strip` to
collapse line breaks and remove surrounding white space.
```ruby
description = <<~MSG.gsub(/\s+/, ' ').strip
This is the description we need to provide for #{user.name} as part
of an API request dealing with compliance and registration for a
service. If you need to contact them, their email is #{user.email}.
MSG
#=> "This is the description we need to provide for #{user.name} as part of an API request dealing with compliance and registration for a service. If you need to contact them, their email is #{user.email}."
```
Or in a Rails context, I can instead just use `squish`:
```ruby
description = <<~MSG.squish
This is the description we need to provide for #{user.name} as part
of an API request dealing with compliance and registration for a
service. If you need to contact them, their email is #{user.email}.
MSG
#=> "This is the description we need to provide for #{user.name} as part of an API request dealing with compliance and registration for a service. If you need to contact them, their email is #{user.email}."
```