How to check if two strings are equal in Ruby

Overview

In this shot, we will check if two strings are equal with the eql?() method. Two strings are equal if they both have the same length and content with the same case.

Syntax

str.eql?(other_str)

Parameters

other_str: The string we compare with the string instance.

Return value

The value returned is a Boolean. It returns the true if the strings are equal. Otherwise, false is returned.

Example

# compare strings
puts "one".eql?("one"); # true
puts "two".eql?("two"); # true
puts "three".eql?("three"); # true
puts "FIVE".eql?("five"); # false
puts "six".eql?("Six"); # false

Explanation

All the first three string comparisons return true because they are equal. The rest are not equal because they are not of the same case. That is why they will return false.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources