Home » Ruby programming

Ruby Comments

Ruby Comments: Here, we are going to learn about the comments in Ruby programming language, types of the comments (single line comment and multiline comments in Ruby).
Submitted by Hrithik Chandra Prasad, on July 26, 2019

Ruby comments

Before learning how comments are applied in Ruby? Let us understand what is exactly meant by comments in programming and what makes them so important.

The main aim of comments is to make the source code easier for the programmers or developers to understand. They are a kind of documentation for the program which is done to remind the readers about the tricky logics which have been applied during the process of writing the source code. They are not generally processed by compilers or interpreters. They are not typed in programming languages; Human-friendly language is preferred to make a comment entry.

Various purposes are listed behind making a comment entry:

  • Accommodates metadata: Comments accommodate metadata of the source code. It tells about the original and version, name of the developer, current owner, etc.
  • Act as a code descriptor: A code description is employed by the programmer to make the readers understand her logic. It tells the summary of the code.
  • Helps in debugging: It provides a great help while debugging a block of code. We apply a print statement to see what the output of the particular block or module is so that we don't have to face logical errors in our program. It becomes very hectic for a programmer to find where the logical issue is happening. After successful debugging, we make the print statement as the comment entry.
  • Helps in modification: It helps in the future modification of the code by letting the developer know about the module in which the coder should make changes to fulfill the current or required objective.

Comments in Ruby

Writing comments is considered as a good practice. Comments are also helpful if you temporarily want to disable your code as they are not interpreted or compiled. There are two types of comment which you will need while programming a Ruby code:

  1. Single line Comment
  2. Multi-line Comment

1) Single line Comment

You can use single line comment at the end of the line of code or inline to make the line more understandable to the reader. Remember the following two points about the Single-line comments:

  1. They start with # symbol. It is known as the pound symbol.
  2. As a good practice, we put space between the starting (#) and elements of the comment.

Syntax:

    # (elements of the comment)

Example:

puts "Enter Roll No"
#local variable 'roll'
roll = gets.chomp 

puts "Enter Name"
name = gets.chomp

puts "Enter percentage"

#conversion of string into integer
per = gets.chomp 		

if (per.to_i > 40)
	puts "Congratulations #{name} Roll no #{roll}! Your percentage exceeds passing criteria"
else
	puts "Hi #{name} Roll no #{roll}.You are fired!"
end

Output

Enter Roll No
101
Enter Name
Hritik
Enter percentage
88
Congratulations Hritik Roll no 101! Your percentage exceeds passing criteria 

In the above example, you can observe that we have two comment entries i.e. "local variable 'roll' (telling about the variable) and "Conversion of String into an integer" (through .to_i method).

2) Multi-line Comment

Multi-line comments are also known as Block comments. You can make multi-line comments by writing multiple pound # symbols like this:

    # (first line of the comment)
    # (second line of the comment)
    # (third line of the comment)

Example:

    #puts "Hello World!"
    #puts "Cow gives us milk."
    #puts "Satyam is a crazy guy."

You can also select and comment all of it by using =begin ... =end. But only modern code editors allow this feature.

Syntax:

    =begin
    comment entry 1
    comment entry 2
    comment entry 3
    =end

Example:

roll = gets.chomp                          
puts "Enter Name"
name = gets.chomp
puts "Enter percentage"
per = gets.chomp 	

=begin					
if (per.to_i > 40)
	puts "Congratulations #{name} Roll no #{roll}! Your percentage exceeds passing criteria"
else
	puts "Hi #{name} Roll no #{roll}.You are fired!"
end
=end

puts "You are caught!"

Output

Enter Roll No
101
Enter Name
Hritik
Enter percentage
88
You are caught!

In the above example, the if...end block does not compiler and the last statement is printing as output.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.