Ruby program to compare numbers and strings using library function

Ruby Example: Write a program to compare numbers and strings using library function.
Submitted by Nidhi, on December 15, 2021

Problem Solution:

In this program, we will create some variables. Then we will compare numbers and strings using library function eql?(). The eql?() function returns Boolean value true or false after comparison.

Program/Source Code:

The source code to compare numbers and strings using the library function is given below. The given program is compiled and executed successfully.

# Ruby program to compare numbers and 
# strings using library function

num1 = 22;
num2 = 3;
num3 = 3;

str1 = "hello";
str2 = "hello";
str3 = "hi";

print num1.eql?(num2),"\n";
print num2.eql?(num3),"\n";

print str1.eql?(str2),"\n";
print str2.eql?(str3),"\n";

Output:

false
true
true
false

Explanation:

In the above program, we created some variables. Then we performed a comparison between numbers and strings using the eql?() function and printed the returned Boolean value.

Ruby Basic Programs »




Comments and Discussions!

Load comments ↻





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