Avoid interpolation in heredoc
Published in
1 min readMar 8, 2018
--
Heredoc with interpolation
By default, heredoc
accepts interpolation and special characters
For example
venue = "world"str = <<-EOF
I'm the master of the #{venue} !\n
No you're dead bro..
EOF
Output
I'm the master of the world !No you're dead bro..
Heredoc without interpolation
In order to avoid interpolation you have to surround the heredoc
identifier by quotes. Notice the <<-'EOF'
in the following example
venue = "world"str = <<-'EOF'
I'm the master of the #{venue} !
No you're dead bro..\n
EOF
Output
I'm the master of the #{venue} !
No you're dead bro..\n
Misc
Some text editors understand heredoc
and pretty print the string depending on the heredoc
flag.
Here is a list of common heredoc
flags: <<-SQL
, <<-HTML
, <<-XML
, <<-JSON
<<-SQL
SELECT id
FROM table
WHERE
id IS NOT NULL AND id > 200
ORDER BY
id DESC;
SQL
Ruby Mastery
We’re currently finalizing our first online course: Ruby Mastery.
Join the list for an exclusive release alert! 🔔
Also, you can follow us on x.com as we’re very active on this platform. Indeed, we post elaborate code examples every day.
💚