Question
How can I comment multiple lines in Ruby?
For example, if I want to temporarily disable a block of Ruby code or add a longer explanation across several lines, what is the correct way to do that in Ruby?
Short Answer
By the end of this page, you will understand how Ruby handles comments, how to write comments across multiple lines, when to use # versus =begin and =end, and which approach is most practical in real Ruby codebases.
Concept
In Ruby, comments are used to write notes for humans without affecting how the program runs.
Ruby supports two main comment styles:
- Single-line comments with
# - Block-style comments using
=beginand=end
A single-line comment looks like this:
# This is a comment
For multiple lines, the most common approach is simply to put # at the start of each line:
# This is line one
# This is line two
# This is line three
Ruby also supports a special document-style block comment syntax:
=begin
This is a multi-line comment.
It can span several lines.
=end
However, =begin and =end have an important rule:
- They must start at the beginning of the line
- They are used less often in everyday Ruby code
In real projects, developers usually prefer repeated # comments because they are clearer, easier to format, and better supported by editors and style guides.
This matters because comments are part of code readability. Good comments help explain intent, temporarily disable code during debugging, and document tricky logic. But using the right comment style makes code easier for other Ruby developers to understand and maintain.
Mental Model
Think of comments like sticky notes attached to your code.
- A
#comment is like putting a small sticky note on one line. - Several
#lines in a row are like stacking multiple sticky notes to explain a bigger idea. =beginand=endare more like putting a large paper cover around a whole section.
Ruby programmers usually prefer the stack of small sticky notes because they are easier to move, edit, and read.
Syntax and Examples
Single-line comment
# Print a greeting
puts "Hello"
The line starting with # is ignored by Ruby.
Multi-line comment using #
# This method calculates the total price.
# It includes tax and shipping.
# Use it when showing the checkout summary.
def total_price(subtotal, tax, shipping)
subtotal + tax + shipping
end
This is the most common Ruby style for multi-line comments.
Multi-line comment using =begin and =end
=begin
This whole section is treated as a comment.
Ruby ignores everything until it reaches =end.
=end
puts "Hello"
Temporarily disabling code
# puts "Debug: starting process"
# puts "Debug: loading file"
puts "App is running"
This is often how developers temporarily disable code while testing.
Step by Step Execution
Consider this example:
# Start of program
# The next line prints a message
puts "Hello, Ruby!"
Step by step:
- Ruby reads
# Start of program- Because the line starts with
#, Ruby ignores it.
- Because the line starts with
- Ruby reads
# The next line prints a message- This is also ignored.
- Ruby reads
puts "Hello, Ruby!"- This is executable Ruby code.
- Ruby prints
Hello, Ruby!
Now look at a block comment example:
=begin
This is all ignored.
So is this line.
=end
puts "Done"
Step by step:
- Ruby sees
=begin- It starts a block comment.
- Ruby ignores all following lines.
- Ruby sees
=end- The block comment stops.
- Ruby reads
puts "Done"
Real World Use Cases
Comments across multiple lines are useful in many practical situations:
- Explaining business logic
- For example, why an invoice total includes a special discount rule.
- Documenting temporary workarounds
- Example: a note explaining why a piece of code exists until an external API bug is fixed.
- Disabling debug code
- Developers often comment out several
putsstatements while testing.
- Developers often comment out several
- Adding setup instructions in scripts
- A Ruby script may include several comment lines describing required environment variables.
- Annotating examples in teaching code
- Tutorial or sample Ruby files often use multiple
#lines to explain each section.
- Tutorial or sample Ruby files often use multiple
Example:
# This script imports customer data.
# Expected input: CSV with headers.
# Rows with missing email addresses are skipped.
load_customers("customers.csv")
Real Codebase Usage
In real Ruby codebases, developers usually use multi-line comments in a few standard ways:
Repeated # for explanations
This is the most common pattern:
# We return early here because guest users
# should not be allowed to update billing details.
return if current_user.guest?
Commenting out code during debugging
# puts response.body
# puts response.status
process_response(response)
Guard clauses with explanation
# Skip processing if the file does not exist.
# This prevents a downstream parser error.
return unless File.exist?(path)
Validation notes
# Names are limited to 50 characters because
# the external billing service rejects longer values.
raise ArgumentError, "name too long" if name.length > 50
Why developers avoid =begin/ in teams
Common Mistakes
1. Assuming Ruby has C-style comments
Ruby does not use /* ... */.
Broken example:
/* This is not valid Ruby */
puts "Hello"
Use this instead:
# This is valid Ruby
puts "Hello"
2. Indenting =begin and =end
Broken example:
=begin
Comment text
=end
This may not work as expected because =begin and =end should start at the beginning of the line.
Correct version:
=begin
Comment text
=end
3. Using =begin/=end inside code blocks carelessly
Comparisons
Comment styles in Ruby
| Style | Syntax | Best for | Common in real code? | Notes |
|---|---|---|---|---|
| Single-line comment | # comment | One-line notes | Yes | Most common |
Multi-line with repeated # | # line 1# line 2 | Explanations over several lines | Yes | Preferred style |
| Block comment | =begin ... =end | Larger commented sections | Less common | Must start at beginning of line |
vs /
Cheat Sheet
Quick reference
Single-line comment
# This is a comment
Multi-line comment, preferred style
# First line
# Second line
# Third line
Block comment syntax
=begin
This is a block comment.
It spans multiple lines.
=end
Rules
- Use
#for normal comments - Use multiple
#lines for multi-line explanations =beginand=endmust start at the beginning of the line- Ruby does not use
/* ... */ - Prefer comments that explain why, not obvious code behavior
Good practice
# Return early if the user is not logged in.
return unless current_user
Avoid
FAQ
How do you write a multi-line comment in Ruby?
The most common way is to put # at the start of each line. Ruby also supports =begin and =end for block comments.
Does Ruby support /* ... */ comments?
No. That syntax is used in languages like JavaScript, C, and Java, but not in Ruby.
Should I use =begin and =end in Ruby?
You can, but most Ruby developers prefer multiple # lines because they fit better with indentation and everyday coding style.
Why is =begin not working in my Ruby code?
A common reason is indentation. =begin and =end should start at the beginning of the line with no leading spaces.
What is the best way to comment out several lines in Ruby?
Usually, add # to each line. Most editors can do this automatically for selected lines.
Can I use multi-line comments inside a method in Ruby?
Yes. The easiest and most readable way is to use multiple # lines inside the method body.
Are comments ignored when Ruby runs?
Mini Project
Description
Build a small Ruby script that demonstrates the different ways to comment code. This helps you practice single-line comments, multi-line explanations with #, and block comments with =begin and =end while keeping the script runnable.
Goal
Create a Ruby script that prints a short message and includes both single-line and multi-line comments correctly.
Requirements
- Write a Ruby script that prints at least one message with
puts. - Add a multi-line explanation using
#on each line. - Include a valid
=beginand=endblock comment. - Comment out one
putsline so it does not run.
Keep learning
Related questions
How to Call Shell Commands from Ruby and Capture Output
Learn how to run shell commands in Ruby, capture output, check exit status, and choose the right method for scripts and apps.
How to Check Whether a String Contains a Substring in Ruby
Learn how to check if a string contains a substring in Ruby using include?, match, and multiline string examples.
How to Check if a Hash Key Exists in Ruby
Learn how to check whether a specific key exists in a Ruby hash using key?, has_key?, and include? with clear examples.