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 HereDoc problem

Hi Everyone,

I'm having issues with the ruby heredoc (Ruby Foundations - Creating a String @ 6:09 seconds).

After i type the heredoc as it states at (6 mins 9 secs) 'name = <<-STR' all i get is a new line. Then nothing else i type works, ie, puts "hello". I then have to restart my terminal then type puts "hello" and only then it works.

Do you guys know why my 'heredoc' doesnt work?? it would be a great help, thanks in advance :)

1 Answer

Hey Vic,

When you use the heredoc command, you're creating a way for you to try a lot of characters to put into one variable. The key part about the heredoc, is you must open and close where you want to use the heredoc.

So first you start with your variable name:

names

next, add the equals character to assign everything to your variable that you'll type in your heredoc. You also need to come up with a word or set of characters to use when you open your heredocs. I chose 'NAMES' to open and close my heredoc. Remember to uppercase every letter, as so:

names = <<-NAMES

then when Ruby gives you a new line, start typing out whatever you want (including the return key):

names = <<-NAMES
joe
bob
nancy
kathy
fred
mike
joe
bob
nancy
kathy
fred
mike

when you're all done, you need to CLOSE your heredoc with how you opened your heredoc:

names = <<-NAMES
joe
bob
nancy
kathy
fred
mike
NAMES
=> "joe\nbob\nnancy\nkathy\nfred\nmike\n"

After you close the heredoc, you'll get the last line which just shows the output that Ruby has.

I hope that helps!

Hi Neil,

thanks for your answer, it was very clear and helped :)

cheers vic