There are several ways we can compare strings in Ruby. Four ways we will look at to do this are using the == equality operator, eql? method, equal? method, and the <=> spaceship operator. Let’s take a look at each one individually first, and then we will show some examples of all of them at the end.
Let’s first take a look at the == equality operator. This is one of the simplest ways to compare two strings. If the two strings are equal, then true will be returned. Otherwise, false.
string_one == string_two
And here is a simple example using the equality operator.
string_one = "This is a string"
string_two = "This is a string"
puts string_one == string_two
#Output
true
Next, let’s take a look at the eql? method. This also returns true if both strings are equal, and false if they are not.
string_one.eql? string_two
And here is a simple example using the eql? method.
string_one = "This is a string"
string_two = "This is a string"
puts string_one.eql? string_two
#Output
true
Next, let’s take a look at the equal? method. This also returns true if both strings are equal, and false if they are not. You will see it returns the same thing as the eql? method.
string_one.equal? string_two
And here is a simple example using the equal? method.
string_one = "This is a string"
string_two = "This is a string"
puts string_one.equal? string_two
#Output
true
For strings, the eql? and equal? methods return the same value. But when working with other object types, like Integers or Hashes, this might not always be the case. So be aware.
Finally, let’s take a look at the spaceship operator, <=>. This operator will compare the left argument to the right. If the left is less than the value on the right, it will return -1. If it is greater than the value on the right, it will return 1. If they both are equal, it will return 0.
So if <=> returns 0, we know that both strings are equal.
string_one <=> string_two
And here is a simple example using <=>.
string_one = "This is a string"
string_two = "This is a string"
puts string_one <=> string_two
#Output
0
Now, let’s test each one of these methods with different string values to see if they do indeed return the same value. Remember all of the operators and methods will return true if the strings are equal, except the spaceship operator, which should return 0.
string_one = "This blog is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes."
string_two = "This blog is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes."
puts string_one == string_two
puts string_one.eql? string_two
puts string_one.equal? string_two
puts string_one <=> string_two
#Output
true
true
true
0
string_one = "1"
string_two = "1.0"
puts string_one == string_two
puts string_one.eql? string_two
puts string_one.equal? string_two
puts string_one <=> string_two
#Output
false
false
false
-1
string_one = "Home"
string_two = "home"
puts string_one == string_two
puts string_one.eql? string_two
puts string_one.equal? string_two
puts string_one <=> string_two
#Output
false
false
false
-1
Note in this last example, that when comparing strings, case is important. If we want to compare strings without caring if they have the same case, we can use the casecmp method.
string_one = "Home"
string_two = "home"
puts string_one.casecmp string_two
#Output
0
The casecmp method will return 0 if the strings are equal, regardless of case.
Hopefully this article has been useful for you to learn how to use Ruby for string comparison.