Comments in Ruby

Statements that are not executed by the compiler and interpreter are called Comments. During coding proper use of comments makes maintenance easier and finding bugs easily.

In Ruby, there are two types of comments:

  1. Single – line comments.
  2. Multi – line comments.

Here, we are going to explain both types of comment with their syntax and example:



Ruby Single-Line Comments –

It is represented by # sign. It is used to denote single line comment in Ruby. Its the most easiest typed comments. When we need only one line of a comment in Ruby then we can use the characters ‘#’ preceding the comment.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to show single line comments 
  
#!/usr/bin/ruby -w
  
# This is a single line comment.
puts "Single line comment above"

chevron_right


Output:

Single line comment above

Ruby Multi-Line Comments –

If our comment is extending in more than one line than we can use a multiline comment.In Ruby, Multi- Line Comment started by using =begin and ended by using =end syntax.

Syntax :

=begin
continues
continues
.
.
.
Commnent ends
=end

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to show multi line comments 
#!/usr/bin/ruby -w
  
puts "Multi line comments below"
  
=begin
Comment line 1 
Comment line 2  
Comment line 3
=end

chevron_right


Output:

Multi line comments below

We can also execute single line comments by using the above syntax as shown below:

=begin Comment line 1 =end


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.