From ec12f7ea8002da7014d3ec9c28061a514e617f92 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 12 Feb 2026 17:30:15 -0600 Subject: [PATCH] Add Make A Long String Of Text Readable as a Ruby TIL --- README.md | 3 +- ruby/make-a-long-string-of-text-readable.md | 38 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 ruby/make-a-long-string-of-text-readable.md diff --git a/README.md b/README.md index 3d969a2..bb4c553 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). -_1736 TILs and counting..._ +_1737 TILs and counting..._ 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) - [List The Running Ruby Version](ruby/list-the-running-ruby-version.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 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) diff --git a/ruby/make-a-long-string-of-text-readable.md b/ruby/make-a-long-string-of-text-readable.md new file mode 100644 index 0000000..a4adb5b --- /dev/null +++ b/ruby/make-a-long-string-of-text-readable.md @@ -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}." +```