Comments are notes written in the code that are completely ignored by the interpreter.
Here's a simple example of comments in Ruby. You can read the rest of the tutorial to learn more.
Example
# Display "Programiz" to the screen
puts "Programiz"
# Output: Programiz
Here, # Display "Programiz" to the screen
is a comment. Ruby skips this line while running the code.
Types of Comments in Ruby
There are two ways to add comments to code:
#
- Single-line Comments=begin ... =end
- Multiline Comments
Single Line Comments
Any line that starts with #
is a single-line comment. For example,
name = "John Doe"
# Display name on the console
puts name
Output
John Doe
Here, # Display name on the console
is a single-line comment. Ruby completely ignores it when running the code.
Notice that the comment is written on the line just before the code it explains. You can also use single-line comments right beside the code:
name = "John Doe"
puts name # Display name on the console
However, avoid using comments this way if they are long and descriptive.
Note: To comment a line of code, press Ctrl + /
for Windows and Cmd + /
for Mac. To uncomment, either remove the #
symbol manually or press the same shortcut again — it toggles the comment on and off.
Multiline Comments
You can add multiline comments in two ways:
1. Using multiple # symbols.
The most common way to write a multiline comment in Ruby is by using several single-line comments one after another. For example,
# This is a multiline comment
# created using multiple single-line comments
# The code prints "Hello, World!"
puts "Hello, World!"
Output
Hello, World!
2. Using =begin and =end.
Ruby also allows you to write block comments using =begin
and =end
. Everything between these two lines will be treated as a comment.
=begin
This is a block comment.
It can span multiple lines.
Commonly used for documentation or temporarily disabling code.
=end
puts "Hello, World!"
Output
Hello, World!
Note: =begin
and =end
must be placed at the beginning of a line with no indentation and nothing else on the line.
More on Ruby Comments
To use =begin
and =end
for block comments, you must remember two simple rules:
- They must be at the very beginning of the line (no spaces before them).
- They must be on their own lines, with nothing else written on the same line.
If not used properly, Ruby will not recognize them as comments and may show errors:
=begin
This won't work!
=end
So, whenever you use =begin
and =end
, make sure they're cleanly placed at the start of a line — no extra spaces!
Comments can be helpful if you want to prevent the execution of code that can still be useful in the future. Consider the program below:
puts "Hello John Doe!"
puts "Welcome to our Ruby tutorial."
Suppose the line puts "Welcome to our Ruby tutorial."
isn't required right now. But you know you might change your mind in the future.
In this case, you can simply convert the unnecessary line into a comment instead of deleting it from your program.
puts "Hello John Doe!"
# puts "Welcome to our Ruby tutorial."
You can then uncomment the code whenever you need to use it.
This method is also useful for debugging since you can isolate code that may cause issues without deleting them.
Why Use Comments?
As a developer, you'll write code and also need to update or review code written by others.
If you write comments on your code, it'll be easier for you (and your fellow developers) to understand it in the future.
As a rule of thumb, use comments to explain why you did something rather than how you did something.
Note: Comments shouldn't be used for explaining poorly written code. Your code should always be well-structured and self-explanatory.