1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-19 15:08:02 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Bob Conan
957987e09f Merge 5615da920f into 6af86bd407 2024-11-25 17:01:07 -05:00
jbranchaud
6af86bd407 Add Stack Heredocs In A Method Call as a Ruby TIL 2024-11-25 16:00:16 -06:00
Bob Conan
5615da920f Update README.md, fix typos 2024-11-15 16:16:31 -06:00
BobConanDev
c60c63f554 Updated README.md, fix typo(s) 2024-11-15 16:42:57 -05:00
2 changed files with 41 additions and 3 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1516 TILs and counting..._
_1517 TILs and counting..._
---
@@ -190,7 +190,7 @@ _1516 TILs and counting..._
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
- [Check For Cached Site Assocation File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
- [Check For Cached Site Association File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
@@ -724,7 +724,7 @@ _1516 TILs and counting..._
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
- [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md)
- [Check Table For Any Orphaned Records](postgres/check-table-for-any-orphaned-records.md)
- [Checking Inequality](postgres/checking-inequality.md)
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md)
@@ -1294,6 +1294,7 @@ _1516 TILs and counting..._
- [Specify How Random Array#sample Is](ruby/specify-how-random-array-sample-is.md)
- [Split A Float Into Its Integer And Decimal](ruby/split-a-float-into-its-integer-and-decimal.md)
- [Squeeze Out The Extra Space](ruby/squeeze-out-the-extra-space.md)
- [Stack Heredocs In A Method Call](ruby/stack-heredocs-in-a-method-call.md)
- [String Interpolation With Instance Variables](ruby/string-interpolation-with-instance-variables.md)
- [Summing Collections](ruby/summing-collections.md)
- [Triple Equals: The Case Equality Operator](ruby/triple-equals-the-case-equality-operator.md)

View File

@@ -0,0 +1,37 @@
# Stack Heredocs In A Method Call
When you put a heredoc directly in a method call as an argument, it is only the
opening identifier that goes in the argument list.
That looks like this:
```ruby
execute_in_transaction(<<~SQL)
update reading_statuses
set status = 'abandoned'
where started_at < (now() - '2 years'::interval)
and finished_at is null;
SQL
```
You might imagine then that we can put multiple heredocs in a method call. That
leads to [_stacked
heredocs_](https://www.visualmode.dev/ruby-operators/heredoc#stacked-heredocs).
```ruby
execute_in_transaction(<<~SQL1, <<~SQL2, <<~SQL3)
update reading_statuses
set status = 'abandoned'
where started_at < (now() - '2 years'::interval)
and finished_at is null;
SQL1
insert into activity_log (name, description)
values ('abandon_books', 'Mark unread books as abandoned');
SQL2
delete from background_jobs
where id = #{job_id}; -- better to sanitize values like this
SQL3
```
Notice we terminate the body of each heredoc with its closing identifier and
immediately begin the body of the next one.