Exercise 2: Comments and Pound Characters

Comments are very important in your programs. They are used to tell you what something does in English, and they are used to disable parts of your program if you need to remove them temporarily. Here's how you use comments in Ruby:

1
2
3
4
5
6
7
8
9
# A comment, this is so you can read your program later.
# Anything after the # is ignored by ruby.

puts "I could have code like this." # and the comment after is ignored

# You can also use a comment to "disable" or comment out a piece of code:
# puts "This won't run."

puts "This will run."

From now on, I'm going to write code like this. It is important for you to understand that everything does not have to be literal. Your screen and program may visually look different, but what's important is the text you type into the file you're writing in your text editor. In fact, I could work with any text editor and the results would be the same.

What You Should See

$ ruby ex2.rb
I could have code like this.
This will run.

Again, I'm not going to show you screenshots of all the terminals possible. You should understand that the preceding is not a literal translation of what your output should look like visually, but the text between the first $ ruby ... and last $ lines will be what you focus on.

Study Drills

  1. Find out if you were right about what the # character does and make sure you know what it's called (octothorpe or pound character).
  2. Take your ex2.rb file and review each line going backward. Start at the last line, and check each word in reverse against what you should have typed.
  3. Did you find more mistakes? Fix them.
  4. Read what you typed above out loud, including saying each character by its name. Did you find more mistakes? Fix them.

Common Student Questions

Are you sure # is called the pound character?
I call it the octothorpe because that is the only name that no one country uses and that works in every country. Every country thinks its name for this one character is both the most important way to do it and the only way it's done. To me this is simply arrogance and, really, y'all should just chill out and focus on more important things like learning to code.
Why does the # in puts "Hi # there." not get ignored?
The # in that code is inside a string, so it will be put into the string until the ending " character is hit. Pound characters in string are just considered characters, not comments.
How do I comment out multiple lines?
Put a # in front of each one.
I can't figure out how to type a # character on my country's keyboard. How do I do that?
Some countries use the Alt key and combinations of other keys to print characters foreign to their language. You'll have to look online in a search engine to see how to type it.
Why do I have to read code backward?
It's a trick to make your brain not attach meaning to each part of the code, and doing that makes you process each piece exactly. This catches errors and is a handy error-checking technique.

Buy DRM-Free

When you buy directly from the author, Zed A. Shaw, you'll get a professional quality PDF and hours of HD Video, all DRM-free and yours to download.

$29.99

Buy Directly From The Author

Or, you can read Learn Ruby the Hard Way for free right here, video lectures not included.