Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Basics (Retired) Ruby Strings What are Strings?

Lee Stetson
Lee Stetson
16,944 Points

Why does Jason refer to multiple lines with double quotes as cumbersome to type compared with a heredoc?

A heredoc requires more characters and a less natural (at least for the beginner) syntax. What about the heredoc makes it easier to type than the double quotes?

Chris Brennan
Chris Brennan
Courses Plus Student 551 Points

I was thinking the same. I kept going back on the video thinking I missed something.

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Can't think of any advantages in the context of this video, so I don't know why he calls it cumbersome. But heredoc can sometimes be useful because it can retain indentation, allows using double quotes without escaping them and you can also use some additional formatting functions with it:

http://rubyquicktips.com/post/4438542511/heredoc-and-indent

http://en.wikibooks.org/wiki/Ruby_Programming/Here_documents

Lee Stetson
Lee Stetson
16,944 Points

I certainly see the benefits of heredocs, and I appreciate the extra resources you offer. Maybe Jason simply mispoke.

Additionally, I don't often see heredocs covered in beginning courses on Ruby, are they a common convention?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

This is the first time I see that in a course myself. And I haven't seen it used in practice, since in most cases the presentation is separated from the content.

In the context of a video, I can't see much use. Typically heredoc syntax is great when you have to assign a large amount of output to a single variable. Instead of constantly appending values to the variable, you can type one long heredoc and assign that to the variable.

Some examples are typing in long XML or JSON documents. These types of formats have a lot of double-quoted strings and you either have to constantly escape them with the backslash or otherwise deal with their formatting. If you use heredoc, it doesn't require all that and just adding:

require 'json'

.to_json on the output will make a valid JSON variable.

He is just joking around :)