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 trialLee Stetson
16,944 PointsWhy 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?
3 Answers
Maciej Czuchnowski
36,441 PointsCan'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
16,944 PointsI 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
36,441 PointsThis 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.
Michael Soileau
16,199 PointsIn 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.
Jonathan Shull
Courses Plus Student 12,739 PointsHe is just joking around :)
Chris Brennan
Courses Plus Student 551 PointsChris Brennan
Courses Plus Student 551 PointsI was thinking the same. I kept going back on the video thinking I missed something.