Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Lexical Structure in Ruby 
Comments in Ruby 
Single line comments in Ruby
Multi-Line Comments in Ruby or Embedded Documents
Documentation Comments in Ruby
Frequently Asked Questions
What is Ruby?
Ruby is said to be a combination of which five languages?
What are single-line comments in ruby?
What are block-level / Multi-line comments in ruby?
Is Ruby Case Sensitive?
Last Updated: Mar 27, 2024

Comments in Ruby

Author Gaurav Joshi
1 upvote
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


Working in software development, each engineer could feel that writing comments in your code are like going to the dentist for a regular check-up. We don't have to visit the dentist, but we all know its importance. Similarly, writing comments in ruby is not compulsory. Still, one should, no matter how neat, clean self-explanatory ruby script a developer has written but writing comments to it still be an excellent practice. 

Down the blog, we will understand comments in ruby and how to implement them properly in detail but before that, let’s have a little introduction to ruby.


Ruby logo


Ruby is an open-source dynamic language with natural and easy-to-read/write syntax. Ruby is a language of careful balance and is a perfect blend of five different languages  (Perl, Smalltalk, Eiffel, Ada, and Lisp). Often, its creator  Yukihiro "Matz" Matsumoto considers ruby as a human being in the sense that the way human beings are simple from the outside but complex from the inside. Like human bodies, ruby is simple in appearance but complex on the inside. So I think you get to know in brief about ruby, so let us have a brief idea about lexical structure in ruby, before continuing with our discussion in the comments in ruby.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Lexical Structure in Ruby 

Like Human Languages, Computer languages also follow a lexical structure. A source code of the ruby program consists of tokens. While parsing any program, the ruby-interpreter parses the program as a sequence of these tokens. Tokens in ruby include literal, punctuation, comments, identifiers, and keywords. This article introduces you to one type of token, i.e., Comments in ruby. Tokens also include essential information about the characters that comprise the tokens and the whitespace that separates the tokens.

Comments in Ruby 

Commenting marks content in your program that you want the interpreter to ignore. Comments in ruby help programmers increase the code readability. Comments in ruby work as notes or alongside extra information on what that particular section of code does?. Commenting is not only good practice but is essential when working for an organisation.

Comments in ruby can be of several types:

  • Single line comments in ruby
  • Multi-Level / Block Level comments in ruby
  • Documentation Comments in ruby

Now down the section, we will understand single-line and multi-line comments in ruby in brief with example.

Single line comments in Ruby

Anything begins with a # character in comments in Ruby and continues till the end of the Line. The Ruby interpreter ignores any text which follows a “ # ” character but does not ignore the newline character, which is meaningful whitespace and may serve as a statement terminator. If a “ # ” character appears within a string or regular expression literal in ruby, then it is simply part of that string or regular expression and does not consider a comment:

x = "#This is a string with a # character"  # This is a comment
y = /#This is a regular expression/  #  This is another comment
#  And this is also a comment


We could also use a single Line comment marker to mark multi-line comments, but it's not a good practice. “ # ” could be used as multiline comments in ruby by writing simply by beginning each line with a “ # ” character, as shown in the example.

# This represents the first line. 
# This is the second Line.
# This is the third Line.

We will discuss embedded documents or multiline comments in the next section

Multi-Line Comments in Ruby or Embedded Documents

Multiline comments in ruby, also known as an embedded document. Multiline comments in ruby start on a line that begins with  “=begin” and go on till (and include) a line that begins “=end”. If we wrote any text after  “=begin” and before “=end” is part of the comment. The extra text must be separated from the “=begin” and “=end” by at least one space. Embedded documents are a convenient way to comment out long blocks of code without prefixing each line with a “#” character, as seen in the previous section.

This text is a 
Part of Multiline comments in ruby 
Any code here is a comment 
Ruby interpreter will ignore any text written here

We must ensure that “=” signs are the first characters of each line unless the embedded document will not work. Look into the below program to get clarity on this mistake:

# =begin
We begin the comment from here, but it is now a comment since the first character is a #; any text behind # is considered a comment.
The code that goes here is no longer a comment 
# =end 

Embedded documents, as goes by their name, can include long blocks of documentation within a program. Embedded documents are planned to be used by some postprocessing tool run over the Ruby source code. It is typical to follow  “=begin” with an identifier indicating the tool the comment is intended for.

Documentation Comments in Ruby

We can have API documentation as specially formatted comments in ruby that precede method, class, and module definitions. We can browse through this documentation using the RI tool. To extract documentation comments from the ruby source and to format them as HTML or prepare them for display by RI, we use the rdoc tool to extract them. 

Explain that documentation of the rdoc tool is beyond the scope of this article; You could look into the file lib/rdoc/README in the Ruby source code for more details. Documentation comments in ruby should immediately come before the class, module, or any method whose API we document. Documentation comments in ruby are usually written as multiline comments, each line beginning with #. Still, they could also be written as the embedded documents explained above, which start =begin rdoc. 

Look for the below comment program in ruby to demonstrate the essential formatting elements of the markup grammar used in Ruby’s documentation comments.


# Rdoc comments use a simple markup grammar.
# They mark separate paragraphs with a blank line. 
# = Headings 
#  All Headings in the document must begin with an equal sign 
# == Sub-Headings 
# For using Sub-Headings use double equal sign.The line above produces a subheading. 
# === Sub-Sub-Heading 
# And so on. 
# = Examples
# = For using Lists and Fonts 
# Any List Items that begin with * or - is idicating a fonts with punctuation: 
# * _italic_ or Used for creating multi-word italic 
# * *bold* or Used for creating multi-word bold 
# * +code+ or Used for creating multi-word code 
# 1. Any Number list must begin with numbers. 
# 99. Any number will be acceptable; numbers need not have to be sequential. 
# 1. We cannot make nested lists. 
# For making description list each terms are bracketed: 
# [item 1] Description for item 1 
# [item 2] Description for item 2 

Frequently Asked Questions

What is Ruby?

Ruby is an open-source dynamic language with natural and easy-to-read/write syntax. Ruby is a careful balance language and a perfect blend of five different languages.

Ruby is said to be a combination of which five languages?

Ruby, an open-source dynamic language, is a perfect blend of five different languages (Perl, Smalltalk, Eiffel, Ada, and Lisp).

What are single-line comments in ruby?

Any line of code that begins with a # character is a single-line comment in ruby. And each single line comment continues till the end of that Line. The Ruby interpreter ignores any text which follows a # character. 

What are block-level / Multi-line comments in ruby?

Multiline comments in ruby are also called embedded documents. It starts from a line that begins with   “=begin” and goes on till (and includes) a line that begins “=end”.

Is Ruby Case Sensitive?

Ruby is a case-sensitive language, which means lowercase identifiers in ruby are different from uppercase identifiers. E.g. Like_This is entirely different from LIKE_THIS. Similarly, Start is different from START.


In this article, we have studied Comments in Ruby in detail and briefly explored how one can use them to make code more readable

Also, visit our Guided Path in  Coding Ninjas Studio to learn about  Ruby. If you are preparing for an interview, visit our Interview Experience Section and interview bundle for placement preparations. Upskill yourself in Ruby on RailsBackend Web TechnologiesHadoopSQL, MongoDB, Data Structures and Algorithms, JavaScript,  System Design, and much more!. 
To learn more, refer to Operational DatabasesNon- relational databasesMongoDBTop-100-SQL-problemsinterview-experienceIntroduction to ruby on railsDirectory Structure in RubyRuby on RailsRuby vs Python. You can also refer to the Official RubyRubyOfficial DocumentationRuby FAQRuby KoansRuby DocWhy Ruby?

We expect that this article must have helped you enhance your knowledge on the topic of Ruby. Please upvote our blogs if you find them engaging and helpful! 

We wish you all the best for your future adventures.

Happy Coding!!!


Next article
Whitespace in ruby
Live masterclass