Learn Ruby the Hard Way – Exercise 24 More Practice

Here is my work for Exercise 24.

Exercise 24 code
Exercise 24 code
Exercise 24 output
Exercise 24 output

Study Drills
1. Make sure to do your checks: read it backward, read it out loud, and put comments above confusing parts.

The part that was new here was the use of heredoc on line 4. (Here document) It is another way (other than triple double quotes) to create multi-line strings.

2. Break the file on purpose, then run it to see what kinds of errors you get. Make sure you can fix it.
I removed the END on line 11. I got two lines.

ex24.rb:38: can’t find string “END” anywhere before EOF
ex24.rb:7: syntax error, unexpected end-of-input, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END

Ruby is saying it cannot find the END string anywhere before the end of the file. (in the rest of the file). It is telling me that there is a syntax error, it was expecting the string “END” to end the heredoc, but then the input ended. By having matching token strings at the beginning and end of the heredoc, this problem is fixed.

END is simply a token string. We can even replace both of the END words with hamburger, the code will still work the same. All that matters is that the token strings are the same.

This exercise introduced me to heredoc in Ruby. I also reviewed using escape sequences, creating functions with parameters, using returned values from functions, and string interpolation.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s