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:
- Single – line comments.
- 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 :
Ruby
# Ruby program to show single line comments #!/usr/bin/ruby -w # This is a single line comment. puts "Single line comment above" |
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 . . . Comment ends =end
Example :
Ruby
# 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 |
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
Please Login to comment...