Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Basics (Retired) Ruby Strings String Creation

Sebastian Röder
Sebastian Röder
13,878 Points

Quiz question about multiline strings confusing

The expected answer to the following quiz question is True:

# The following code would create a valid string in Ruby with 3 lines in it:

string = "
My Ruby string here
Is created in three lines
This is a haiku"

I gave the answer False because the code sample actually creates a string with 4 lines in it (an empty line at the beginning and than the 3 lines of text. I was a little confused that my answer was not accepted as correct.

I would recommend formatting the multiline string in the example with the first line of text following immediately after the opening "(the same way it is done in the video) to avoid any confusion, because then the string actually has 3 lines instead of 4.

# The following code would create a valid string in Ruby with 3 lines in it:

string = "My Ruby string here
Is created in three lines
This is a haiku"

2 Answers

Cristian Altin
Cristian Altin
12,165 Points

I agree. Pasting the code in the irb gave this output: => "\nMy Ruby string here\nIs created in three lines\nThis is a haiku"

It's 4 lines. The question would be correct by saying that it would create a valid string in Ruby with 3 newlines "\n" in it. But also the string text would need to change and would lose being a haiku (which btw requires three lines with 5 syllables in the first and 7 in the second and 5 in the third).

Hence your suggestion is correct IMHO.

I agree as well. The newline at the beginning of the code produces a blank line and then three more lines after that, which for me, reads four lines, not three. Your code suggestion makes sense - remove the newline, and then the string is properly three lines long.