LRTHW Exercise 7, 8 and 9

These are primarily exercises to type code given and troubleshoot it to make it run as intended. The book indicates there’s going to be a few of these now so as to practice debugging, so I’ve grouped them here together.


Exercise 7

So here’s the code that I’ve been given off the bat, with my comments inserted as to how I think it’s going to run. (Sidenote: I’m inserting comments in most lines now without prompting as I’m writing the code as it’s best practice and helps me remember why I put something in.)

I had questions about this one

This was the output:

  • My guess about prints vs puts was correct.
  • Initially when I typed out this code I didn’t put the single quotation marks around snow and assumed that it was a variable that was unestablished and thus was going to break the code (it did), but going back and, as advised, reading the code backwards line by line made me see my error. I still don’t understand why you would write it like this, the book gives this explanation:

(a) it’s not a variable, it is just a string with the word snow in it (a variable wouldn’t have the single quotes

(b) In Ruby the “” (double quotes) tell Ruby to replace variables it finds with the #{} but the ‘’ (single quotes) tells Ruby to leave the string alone and ignore any variables inside it.

The end of this exercise cautions you to keep track of your mistakes and to try not to make them in future, but shit happens. My mistake here was getting too ahead of myself and assuming a ‘variable’ wouln’t work when I didn’t read it properly and didn’t understand what it was meant to do.

Exercise 8

The initial notes for this exercise says that it’s for how to create a format string (one in which you use values by their names instead of variables). Upon first reading I was confused as to the introduction/user of the %{} symbol without warning, so I looked it up and it’s called a percent literal (more context in Wiki). Reading this helped me understand the eventual execution for the code below.

Code with comments
Output

When you put formatter inside the formatter function again it prints the absolute value of what’s already in the variable. Inception!

Note from end of the exercise re: should I use %{} or #{} for formatting? You will almost always use #{} to format your strings, but there are times when you want to apply the same format to multiple values — that’s when the %{} comes in handy.

Also, putting quotes around true and false treats them as strings, which is why you don’t put quotation marks around them when using them in a similar context as above as they are recognised by Ruby as representing the concepts of true and false.

Exercise 9

I’m just gonna keep typing this shit as described. I’m still interested in the function of new stuff introduced even though it’s not explained yet, so I added my comments after I ran the code.

code with my comments about usage
Final display

My most common errors so far are syntax errors, eg I miss out an additional quotation mark, forget to close curly brackets, etc. No spelling errors as such as yet. Using a good text editor will reduce this problem (I’m using repl.it to actually run/write my code at the moment but I really like Atom as a local text editor)

Like what you read? Give — a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.