Home » Tutorial: Comments in Ruby

Tutorial: Comments in Ruby

Comments in Ruby are notes or explanations within the code that are ignored by the Ruby interpreter.

They are essential for improving code readability, maintaining projects, and documenting functionality.

Ruby supports single-line and multi-line comments.

What You’ll Learn

  1. Introduction to Comments
  2. Single-Line Comments
  3. Multi-Line Comments
  4. Best Practices for Using Comments
  5. Practical Examples

1. Introduction to Comments

Comments do not affect the execution of the program. They serve as documentation for developers to understand the code better or leave reminders.

2. Single-Line Comments

Single-line comments in Ruby start with the # symbol. Everything after the # on the same line is ignored by the Ruby interpreter.

Example: Single-Line Comments

# This is a single-line comment
puts "Hello, Ruby!"  # This prints a greeting

3. Multi-Line Comments

Ruby does not have a specific syntax for multi-line comments like some other languages, but you can achieve multi-line comments in two ways:

3.1 Consecutive Single-Line Comments

Use # for each line:

# This is a multi-line comment.
# It spans multiple lines.
# Each line starts with a # symbol.
puts "Hello, Multi-Line Comments!"

3.2 Using =begin and =end

Ruby supports =begin and =end for block comments. Text between these markers is treated as a comment.

=begin
This is a multi-line comment.
It spans multiple lines.
No need to prefix each line with #.
=end
puts "Ruby supports block comments!"

4. Best Practices for Using Comments

4.1 Write Meaningful Comments

Comments should clarify code, not restate it.

# Bad comment
x = 10  # Assign 10 to x

# Good comment
x = 10  # Represents the maximum number of retries

4.2 Use Comments to Explain Why

Focus on explaining why certain decisions were made in the code.

# Retry the operation up to 3 times because the API occasionally times out
retries = 3

4.3 Avoid Obvious Comments

Don’t comment on things that are self-explanatory.

# Bad comment
puts "Hello"  # Prints Hello

# No comment needed for the above line

4.4 Use Comments for Documentation

Explain the purpose and usage of methods or classes.

# Calculates the square of a number
# @param number [Integer] The number to be squared
# @return [Integer] The square of the number
def square(number)
  number ** 2
end

5. Practical Examples

5.1 Inline Comments for Clarity

balance = 1000  # Initial bank account balance
deposit = 200   # Amount to deposit
balance += deposit  # Update the balance
puts balance

5.2 Disable Code Temporarily

Comments can be used to disable code during debugging.

# puts "This line is disabled temporarily"
puts "This line will execute"

5.3 Use Multi-Line Comments for Larger Explanations

=begin
The following code calculates the factorial of a number.
Factorial is defined as:
n! = n * (n-1) * (n-2) * ... * 1
=end
def factorial(n)
  (1..n).reduce(1, :*)
end
puts factorial(5)  # Output: 120

5.4 Document a Class

# Represents a bank account
class BankAccount
  # Initializes a new bank account
  # @param name [String] The account holder's name
  # @param balance [Float] The initial balance
  def initialize(name, balance)
    @name = name
    @balance = balance
  end

  # Deposits money into the account
  # @param amount [Float] The amount to deposit
  def deposit(amount)
    @balance += amount
  end
end

5.5 Use Comments for TODOs

# TODO: Add error handling for invalid input
def divide(a, b)
  a / b
end

6. Summary

Key Takeaways

  • Use # for single-line comments.
  • Use =begin and =end for multi-line comments when necessary.
  • Write meaningful and concise comments that explain why, not what.
  • Use comments for documentation, debugging, and leaving notes for future enhancements.

Best Practices

  1. Avoid overusing comments—write clear and self-explanatory code.
  2. Regularly update or remove outdated comments.
  3. Document methods and classes for better maintainability.

With proper use of comments, you can make your Ruby code easier to understand, maintain, and debug

You may also like