Avoid interpolation in heredoc

Before to start

I’m thrilled to share with you our latest project: Fun Facts about Ruby — Volume 1

Please feel free to spread the word and share this post! 🙏

Thank you for your time!

Heredoc with interpolation

By default, heredoc accepts interpolation and special characters

For example

venue = "world" str = <<-EOF

I'm the master of the #{venue} !



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..



EOF

Output

I'm the master of the #{venue} !

No you're dead bro..



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

Voilà!

May I have your attention please 🎤🎤

Feel free to subscribe here: www.rubycademy.com