DEV Community

Marty
Marty

Posted on

heredocs theredocs

Welcome to Wikipedia where everything you absolutely need to know in under 30 seconds or else it is worthless is right at your fingertips!

ADS ADS ADS!!! BUY BUY BUY!!! SELL SELL SELL!!!

Alt Text

This is perfectly pure hell. But if you really don't want to read through my ramble of surreal word vomit to make the 6 min mark, scroll down, that's where I actually talk about the content of this post.

tbh, Wikipedia is pretty great. I once had a rad punk programmer professor encourage us to use Wikipedia for sources. So about these heredocs. I was working my way through some Object Relational Mapping course work and one does and in this lesson, it tells me a 'Top-Tip!' about using heredocs for multiple lines of strings that you don't wanna use in your text editor because they will go on FOREVER.

And this lesson has the nerve to hyperlink
heredoc to the actual factual wiki page. Now, the lesson doesn't know this, or maybe isn't really in tune with me, but I'm already behind on studying, and I really don't have the hour+3 to decipher through a Wikipedia rabbit hole. I ain't got time for that!

I know what you are trying to do here, heredocs...

Alt Text

I'm like, oh thanks, thats cool, I'll check it out later. With the real intention of checking the link out later, but now it's buried in a thread of 43 texts in the group chat.

working my way through ORM labs, I keep having to write something similar within methods:

      sql =  <<-SQL 
          CREATE TABLE IF NOT EXISTS tables (
             id INTEGER PRIMARY KEY, 
             name TEXT, 
             kind TEXT
             )
             SQL
      DB[:conn].execute(sql)
Enter fullscreen mode Exit fullscreen mode

And I am completely bamboozled on where this:

               <<-
Enter fullscreen mode Exit fullscreen mode

came from.

So I go back through lessons, and I'm having the hardest time finding where it was explained. I then decide well heck, I'll google this see what comes up
Alt Text

Ohhhh and the search turns up heredoc! I remember that from somewhere???? I find that tiny paragraph about using heredocs ...and I'm still confused. Thankfully, I wasn't the only one. There were many resources, blogs, and posts already created for me and others to check out. Here's my take on it:

1.

<<
Enter fullscreen mode Exit fullscreen mode

this wants the end(closing marker, in this case HEREDOC) to be pushed all the way flush left to the text editor lines.
we can see this written out:

>  str <<HEREDOC
>     our many strings
>HEREDOC

=> "  our many strings"
Enter fullscreen mode Exit fullscreen mode

2.

<<-HEREDOC
Enter fullscreen mode Exit fullscreen mode

gives us the freedom to place spaces or indentations in front of our end(closing marker) HEREDOC. This allows for a cleaner looking code!

>  str <<-HEREDOC
>     our many strings
>  HEREDOC

=> "  our many strings"
Enter fullscreen mode Exit fullscreen mode

3.

<<~HEREDOC
Enter fullscreen mode Exit fullscreen mode

the squiggle acts in almost the same fashion as <<-
it allows us to indent and put spaces anywhere we like within the code, to keep it lookin' pretty. When it is printed to the terminal, it will close any indentations or spaces in the front of the first line.

>  str <<~HEREDOC
>     our many strings
>  HEREDOC

=> "our many strings"
   ^ notice this space is gone!
Enter fullscreen mode Exit fullscreen mode

4.

<<-'NOINTERPOL'
Enter fullscreen mode Exit fullscreen mode

By placing our opening heredoc marker NOINTERPOL in 'single quotes' we are able to opt-out of interpolation, and we are returned a string with no attribute value.

>title = 'There There'
>str = <<-'NOINTERPOL'
>      The book is named #{title}.
>NOINTERPOL

=> "  The book is named #{title}."
Enter fullscreen mode Exit fullscreen mode

And through learning more about how heredocs function, especially with formatting and syntax, I started to think of poetry. How the use of spacing, indentation, size of text, style can have a range of interpretation of infliction. It reminded me of the poem 'Someone In My Family' by Krista Franklin. The use of space as a visual element is also noted in the inflection and pace of the poem. Space changes its meaning, its time, and emotion.
Alt Text

Top comments (0)