Updated March 28, 2023
Introduction to Ruby Comments
Comment in the Ruby are the code which will not execute along with the code, so basically at the time of execution once Ruby see the specific key before the code it consider the code as the comment, and main benefits of this is that, we can write some definition for the lengthy codes and also we can write few description for the used variables and the functions inside the codes, in Ruby we can write two type of the comment one is for the single line(in case defining variables) and another is for the multiple line(in case defining the whole functions).
Types of Ruby Comments
There are mainly two types of the comment in the Ruby they are given below:
1. Single Line Comments
Single line comment are start and end in the one line only , which means once it start with # next new line will not be again comment. Also a single line comments are the comments which are generally used to define any variable or any constant, which means these single line comments are just inserted in between anywhere into the current code. Main benefits of writing the single line comment in the Ruby, it is very flexible we can put anywhere into the code.
Example #1
We can explain the below example in the following steps.
- We defined a method add and description for the add function on the top of the function which start with # symbol.
- Here we are able to put description inside the function also, we have defined the variables a and b inside the function.
- Actually when Ruby found # In the start of the command it understands that it is a single line comment and it will not execute this code block.
- So basically with the help of # we can start the comment in the code section from anywhere.
Code:
#add is the function which will return some of the numbers passed to it.
def add(a, b)
#a and b are the value of two number for addition
returna+b
end
#calling the addition method
sum= add(3,4)
#printing the sum of the two numbers.
puts "The some of the numbers are #{sum}"
Output:
Example #2
We are doing the multiplication of the two numbers and the comment sections are explaining the important sections of the code.
- We have defined a function called multi and inside this function we are performing some multiplication.
- Before the start of the function we have defined or written the comment section which explains the multi function.
- Inside the function we are again explaining with comment to the two variables a and b.
- Finally in the end we are explaining the call of the multi function.
Code:
#multi is the function which returns the multiplications of the two numbers.
def multi(a, b)
#a and b are the value of two number for multiplication
return a*b
end
#calling the multi method
sum = multi(3,4)
#printing the multiplication of the two numbers passed to the multi function, it will return numeric(if inputs are numeric).
puts "The multiplication of the two numbers is #{sum}"
Output:
2. Multi Line Comments
Multi line comments are the comments which can be written in the multiple lines, it can be used to make comments for the multiple lines, multiple lines start with =begin and end with =end . So whatever we write inside the =begin and =end block will be considered as the comment section only. These types of comment should be only used for the defining on the top of the functions or bottom. Because we can write many thing inside it, if we are planning to use the multi line comment section then we should use it at the one place not anywhere inside the codes.
Example #1
Here in this example we are defining all the details of the given function at one place, we have defined everything about the input value of the function multi and also we are able to define function multi also inside the same comment block instead of using a single single line of comment for each section. Once the Ruby found start of =begin command it will consider everything before =end as the non executable code only(comment) .
Code:
=begin
multi function perform the multiplication of the input value a and b
here a and b can be any numeric value for multiplications
we can call multifunction to do multiplication and return numeric value if the input a and b are the numeric.
=end
def multi(a, b)
return a*b
end
sum = multi(3,4)
puts The multiplication of the two numbers is #{sum}"
Output:
Example #2
In the below example again we are able to explain everything on the one comment section , we have written the description about the function and it’s used variable as well as the purposes of the given function .Once the Ruby found start of =begin command it will consider everything before =end as the non executable code only(comment).
Code:
=begin
add is the function which will return some of the numbers passed to it
#a and b are the value of two number for addition
we can call add function function to do addition and it will return integer value if the input a and b are the integer.
=end
def add(a, b)
returna+b
end
sum = add(3,4)
puts "the some of the numbers are #{sum}"
Output:
Conclusion
From this tutorial we learned about the comments and their type in the Ruby language, we learned about the working and structures of both the comments types, which is single line comment and multiple line comments, we learned the situation where we should use single line comment and where we should use multiline comments.
Recommended Articles
We hope that this EDUCBA information on “Ruby Comments” was beneficial to you. You can view EDUCBA’s recommended articles for more information.