Chapter 5: Comments
Sometimes, you want to add helpful notes and instructions to your code for others, even you. Trust me, when you come back to a piece of code you wrote six months ago, it’s often hard to figure out what it does.
Comments are a useful feature in all programming language that let you write plain English text which is not executed when the program runs.
A comment allows you to add notes and annotations to your program. You can use comments to explain what a program is doing and why.
Typically, the why is more important than what, which is self-evident.
In the following example, we add a comment: create and print a variable.
# create and print a variable
name = 'David'
print name
Note that it starts with #
, which tells Ruby that it’s a comment and do not execute it.
If you want to write multi-line comments, start each line with a #
.
# This is a multi-line comment,
# which spans multiple lines.
print 'hello'