ADVERTISEMENT
ADVERTISEMENT

Ruby program to compare two strings using the eql?() method

Ruby Example: Write a program to compare two strings using the eql?() method.
Submitted by Nidhi, on January 01, 2022

Problem Solution:

In this program, we will create three string variables and compare them using eql?() and print the appropriate message. The eql?() method returns true if both strings are equal, otherwise, it returns false.

Program/Source Code:

The source code to compare two strings using the eql?() method is given below. The given program is compiled and executed successfully.

# Ruby program to compare two strings 
# using the eql?() method

Str1 = "Hello";
Str2 = "hello";
Str3 = "Hello";

if (Str1.eql?(Str2))
    print "Str1 and Str2 are equal\n";
else
    print "Str1 and Str2 are not equal\n";
end

if (Str1.eql?(Str3))
    print "Str1 and Str3 are equal\n";
else
    print "Str1 and Str3 are not equal\n";
end

Output:

Str1 and Str2 are not equal
Str1 and Str3 are equal

Explanation:

In the above program, we created 3 variables Str1, Str2, Str3 that are initialized with "Hello", "hello", "Hello".  Then we compared string variables using the eql?() method and printed the appropriate message.

Ruby Strings Programs »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.