Few months ago, I implemented web service that returned HTML. I wanted to looking at the code to be easy on the eye, but when sending over network to remove new lines and extra whitespaces.

Since Ruby 2.3, the squiggly heredoc removes extra indentation. Active Support squish removes all whitespace on both ends of the string, and then changing remaining consecutive whitespace groups into one space. Below example illustrates

def stringify_html
  <<~HTML.squish
    <section class="modal">
      <div class="column">Title</div>
    </section>
  HTML
end
stringify_html
#=> '<section class="modal"> <div class="column">Title</div> </section>'

Thanks for reading!