Ruby

Ruby Compare String

String comparison is the process of evaluating two string literals using conditional expressions returning a Boolean true or false.

In Ruby, we can perform string comparison operations using various methods, as discussed in this tutorial.

1. Equality Operator

Two equal signs denote the equality operator in Ruby. It returns true if both objects are equal and false if otherwise.

var1 = “Hello”
var2 = “Hello”
var1 == var2
=> true

The equality operator does not care as long as both provided objects are valid strings

var1 = 'Hello'
var2 = "Hello"
var2 == var1
=> true

If the strings are not equal, the equality operator will return false.

var1 = "Hello"
var2 = "World"
var1 == var2
=> false

You can do more than compare string variables. You can also compare a string literal as:

"hello" == "zero"
=> false

It is good to note that a numerical string is not directly comparable to an integer value.

2 == "2"
=> false

2. Not Equal

The not equal operator, denoted as !=, returns true if the compared values are not equal and false if equal. It acts as a negation of the equality operator.

"hello" != "hello"
=> false

If the compared values are not equal, it returns true as:

"hello" != "Hello"
=> true

3. Eql? Method

Ruby has a built-in string method called eql?. The eql? method checks if the values are equal in both length and content. The eql? method returns true if the compared values are equal or not.

Examples:

var1 = “HELLO”
var1.eql?("HELLO")
=> true

In this case, both strings are equal in length and content.

The eql? method is case-sensitive and will return false if the casing is not equal

var1 = “HELLO”
var1.eql?("hello")
=> false

Unlike the equality operator that returns true when you compare an integer and float, the eql? method returns false as:

10 == 10.0
=> true
10.eql?10.0
=> false

4. Equal? Method

Unlike the equality operator that checks if both values are equal, the equal? method checks if both operands reference the same object.

var1 = "hello"
var2 = "hello"
var1.equal? var2
=> false

You will notice that the method returns false as the values do not reference the same object. You can get this using object_id as:

var1.object_id
=> 280
var2.object_id
=> 300

An example of both operands pointing to the same object is as shown:

var2 = var1
var1.equal? var2
=> true
var1.object_id
=> 280
var2.object_id
=> 280

5. Spaceship Notation

Ruby also provides a way of comparing strings known as spaceship notation. We express this with a less than sign, an equal sign, and a greater than an operator.

The method returns 0 if the strings match -1 if the left operand is less than the right operand, and 1 if the right operand is greater than the left operand.

"hello" <=> "hello"
=> 0
"HELLO" <=> "hello"
=> -1
"hello" <=> "HELLO"
=> 1

6. Casecmp method

To perform a case insensitive comparison, you can use the casecmp method. It has similar return values to spaceship notation.

"Hello".casecmp "hello"
=> 0

The method ignores case-sensitivity and only compares the content of the strings.

7: Range Comparison

To compare if a character is within a specific range, we can use the range operator, donated using triple equal signs ===

("a".."g") === "d"
=> true
("a".."g") === "z"
=> false

The operator returns true if the character is in the range and false if otherwise.

Conclusion

In this guide, we discussed various ways to compare strings in the Ruby language.

Thank you for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list