Ruby Programming
Coding
Software Development
Programming Languages
Commenting Code

Multi-Line Comments in Ruby?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Ruby does have a block comment syntax, but it is not as common as many newcomers expect. The official block form uses =begin and =end on their own lines. In everyday Ruby code, though, most developers prefer multiple single-line # comments because they work better with editors, tooling, and inline formatting.

The Built-In Block Comment Syntax

Ruby's block comment form looks like this:

ruby
1=begin
2This is a multi-line comment.
3Ruby will ignore all of these lines.
4=end
5
6puts "This line still runs"

There are two important rules:

  • '=begin must start at the beginning of the line'
  • '=end must also start at the beginning of the line'

If you indent them or put extra code on the same line, Ruby will not treat them as comment delimiters.

Why Many Rubyists Still Use #

Even though block comments exist, many Ruby programmers prefer repeating #.

ruby
1# This is a comment block.
2# Each line starts with #.
3# Editors usually handle this style well.
4
5puts "This line still runs"

This style has practical benefits:

  • syntax highlighters usually handle it better
  • editors can toggle it with a shortcut
  • it works naturally with inline explanations and documentation style

So if you ask how Ruby developers usually write multi-line comments, the answer is often "with several # lines," not =begin and =end.

Use the Right Tool for the Goal

There are really three different goals people mean when they ask for multi-line comments:

  • documenting code
  • temporarily disabling code
  • writing long developer notes

For documentation, repeated # comments are usually clearer.

For temporarily disabling a few lines, repeated # comments are also more convenient because editors can add and remove them quickly.

For large hidden blocks, =begin and =end can work, but they are still less common in normal application code.

Do Not Confuse Comments with Strings or Heredocs

Some beginners try to use multiline strings as fake comments.

ruby
1"""
2This is not a real comment.
3It is just a string literal.
4"""

That is not a Ruby comment mechanism. It creates or attempts to create a string expression, which is a different thing entirely.

Likewise, heredocs are for multiline strings, not comments.

Comment Quality Matters More Than Syntax

The real question is not only how to make a multi-line comment. It is whether the comment adds value.

Good comments usually explain:

  • why the code exists
  • why a strange workaround is necessary
  • why a tradeoff was chosen

Bad comments repeat what obvious code already says.

That is why a clean block of # comments often beats a giant =begin section full of redundant narration.

When =begin and =end Are Still Useful

The block form is still valid Ruby and can be useful for:

  • quickly disabling a large region during local experimentation
  • copying large prose notes into a script
  • parser-level commenting where line-by-line # would be tedious

But it remains the less idiomatic day-to-day choice.

Common Pitfalls

  • Indenting =begin or =end, which stops Ruby from treating them as block comment markers.
  • Assuming Ruby uses C-style /* ... */ comments when it does not.
  • Using multiline strings or heredocs as fake comments instead of actual comment syntax.
  • Writing huge comment blocks that repeat obvious code rather than explaining intent.
  • Forgetting that repeated # comments are often the more idiomatic Ruby style.

Summary

  • Ruby supports block comments with =begin and =end.
  • Those markers must start at the beginning of their lines.
  • In everyday Ruby code, repeated # lines are often the preferred style.
  • Multiline strings and heredocs are not comment syntax.
  • Choose the comment style that keeps the code clear and tool-friendly.

Course illustration
Course illustration