Home > Articles > Programming > Ruby

Learn Ruby the Hard Way: Comments and Pound Characters

  • Print
  • + Share This
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. Zed Shaw shows you how you use comments in Ruby, in this chapter from Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World Of Computational Thinking with Code, 3rd Edition.
This chapter is from the book

ex2.rb

 1    # A comment, this is so you can read your program later.
 2    # Anything after the # is ignored by ruby.
 3
 4    puts "I could have code like this." # and the comment after is ignored
 5
 6    # You can also use a comment to "disable" or comment out a piece of code:
 7    # puts "This won't run."
 8
 9    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

Exercise 2 Session

$ 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 above 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.

  • + Share This
  • 🔖 Save To Your Account