Ruby Comments Tutorial

In this section, we will learn what a comment is and how to use it in Ruby.

What is comments in Ruby?

Ruby’s comments are a way of explaining parts of a source code so that we or other developers in later time can read it and learn what that part of the source code does. This is especially useful when working on a large program with thousands of lines of codes where we can easily forget how the algorithms in those source codes are put together.

Note that by default, if we type something in Ruby programs that is not following the rules of programming in Ruby, we will get error! For this reason, Ruby provided comments which allow us to write anything in there and be rest assured that Ruby won’t interpret (read) those words.

In short, comments are for developers and not for Ruby programs!

Note that we can use comments anywhere in the program as long as it doesn’t break the convention in that program.

For example, you can put comments on top of methods but you can’t put a comment between the letters of a variable or a method name, etc.

Types of comments in Ruby:

There are two types of commenting in Ruby.

  • Single line comment
  • Multiline comment

Ruby single line comment

A single line of commenting is used when we want to put a small comment (usually one or two lines) in a source code.

A single line of comment can appear on top of a method, within the body of a method, at the beginning of a program or in the middle of a program to explain a missing part or explain part of the algorithm, etc.

Also, considering the fact that Ruby skips comments, we can use comment in front of a variable for example, if want to discard a variable from a program without actually removing it.

Ruby single line comment Syntax:

# comment…

In order to create a single line comment in Ruby, we start with the # sign and anything that appears after this symbol will be part of the comment and Ruby will skip over that comment.

Note that the entire line starting from the # symbol will be part of the single line of commenting.

Example: creating a single line comment in Ruby

# This is a comment on top of the func method

def func

    #this is a comment within the body of the func method

    print "Hello World"


Ruby multiline comment

On the other hand, we have multi-line commenting where a user can use this syntax to write multi-line comments.

As the name of this type of commenting suggests, we use them when we want to write a long comment (containing multiple lines) on parts of our program.

Ruby multiline comment syntax:




a multi-line



`=begin`: A multiline comment starts with the `=begin`. Note that there’s no space between the equal and the word `begin`.

`=end`: After that we end a multiline comment with another equal sign and the word `end`. Again note that there’s no space between the `=` and `end`.

Note: the area between `=begin` and `=end` is the place where we can put comments.

Example: using multiline comment in Ruby


def func

    print "Hello World"




def func

    print "Hello World"




Hello World

As you can see, we’ve discarded a method and the call to it, simply by surrounding it using a multi-line comment.


Top Technologies