• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Ruby String Comparison

Ruby String Comparison

October 15, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Ruby unshift Method – Add Items to Start of Array
  • 2.  Using Ruby to Convert String to Boolean
  • 3.  Using Ruby to Capitalize First Letter of Each Word
  • 4.  Using Ruby to Capitalize First Letter of String
  • 5.  Ruby Round – Round Number to Nearest Integer with the round Method
  • 6.  Using Ruby to Get the Length of Array
  • 7.  Using Ruby to Reverse String
  • 8.  Ruby Infinite Loop
  • 9.  Ruby has_value Method – Check if Hash Has Value
  • 10.  How to Print With Newline in Ruby

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x