What is eql?() in Ruby?

The eql?() function returns true if both the source number and number in the parameter are equal; otherwise, it returns false.

Figure 1 shows a visual representation of the eql?() function.

Figure 1: Visual representation of eql?() function

Syntax

num_1.eql?(num_2)
# where num_1 is the number which is to be checked against the num_2 for equality

Parameter

The eql?() function takes another number, num_2, to be compared with num_1 (number).

Return value

The eql?() function returns true if both the numbers are equal; otherwise, it returns false.

Code

#equal numbers
print "(2).eql?(2) : ",(2).eql?(2) , "\n"
print "(2.2).eql?(2.2) : ",(2.2).eql?(2.2) , "\n"
#not equal numbers
print "(3).eql?(2) : ",(3).eql?(2) , "\n"
print "(2.1).eql?(2.2) : ",(2.1).eql?(2.2) , "\n"

Free Resources