K. Sabbak

Code Princess

Ruby Heredocs

March 02, 2018

I’ve had a minute to spend more time in Ruby this week and I’m always floored by the wild stuff that this language lets me do. Here’s some of the fancy stuff I learned about HEREDOCs.

Ever find yourself writing something like <<-HEREDOC.gsub(/^ {4}/, '')? Well, apparently after Ruby 2.3, all you have to do is write <<~HEREDOC (note the ~ instead of -) and Ruby will automatically remove the whitespace for you. Specifically it’ll say hey, which of these lines has the least amount of preceding whitespace and then take that amount of whitespace away from all the lines, so further indents are still preserved. It’s amazing.

What about writing something that has a bunch of things you need to escape? Instead of writing all the escape characters yourself, just use <<-'HEREDOC' and Ruby will add in those escape chars for you! It’ll also stop interpolation, so you know, use that one with caution.

How about when you find it annoying to declare and then use your HEREDOC as an argument? Not sure if I like the look of this one but you can also:

"She would never say where she came from".concat(<<-LYRICS)
Yesterday don't matter if it's gone
While the sun is bright
Or in the darkest night
No one knows, she comes and goes
LYRICS

It works. Everything works. It’s amazing. I’m sure there are even more things you can do, but this is what I picked up this week and I wanted to share.

Tags: ruby heredocs