Table of contents
1.
Introduction
2.
Equality operators
3.
== Operator
3.1.
Example
4.
=== Operator
4.1.
Examples
4.1.1.
Example 1
4.1.2.
Example 2
5.
!= Operator
5.1.
Example
6.
=~ Operator
6.1.
Example
6.2.
Various Regular Expressions:
6.3.
Example
7.
!~ Operator
7.1.
Example
8.
Frequently Asked Questions
8.1.
What is the difference between == and === operator?
8.2.
What is the relation between the ‘=~’ and ‘!~’ operators?
8.3.
Is there an operator to perform the inverse of ‘===’ like ‘!==’?
9.
Conclusion
Last Updated: Mar 27, 2024

Equality Operators in Ruby

Author Gurleen Kaur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ruby is an entirely object-oriented, open-source programming language. It has many in-built operators who have different operations. These are mainly Unary operators, arithmetic operators, bitwise operators, logical operators, ternary operators, assignment operators, comparison operators, and range operators. 

This article will consider Ruby's equality operators, which check equality in various cases. Let us go through them in detail.

Equality operators

These are used for checking equality in various conditions in Ruby. It contains ==, ===, !=, ~= and !~. The following table defines the use of these operators.

We will study each operator in detail below.

== Operator

This operator returns true if both the operand's values are the same; else, it returns false.

Example

a = "Hello"
b = "Hello"
puts a == b;

Output:

true

=== Operator

This operator is used with when/case statements. It returns true if the value is in a given range or false.

Examples

Example 1

This example is based on the range use of the === operator.

a=1;
b=5;
c=3;
puts (a..b)===c;

Output:

true

Example 2

This example is based on the implicit use of the === Operator.

marks = 15
case marks
  when 40..90
    puts "pass"
  else
    puts "fail"
end

Output:

fail

!= Operator

This returns true if two operand’s values are exactly not the same; else, it returns false.

Example

a=1;
b=2;
puts a!=b;

Output:

true

=~ Operator

A string of characters known as a regular expression defines a search pattern, typically used in string pattern matching. Ruby regular expressions, often called Ruby regex, enable us to identify specific patterns inside a string. Validation and parsing are two applications for ruby regex. Regex expression declarations in Ruby are made between two forward slashes.

This operator returns the index of the first occurrence of the regex in the string; if present else returns nil.

Note: In the boolean value, nil is said to be false, and any value other than nil is taken as true.

Example

puts "Hello there, Welcome to coding ninjas" =~ /Welcome/
puts "Hello there, Welcome to coding ninjas" =~ /Hi/

Output:

13
       #nil

Various Regular Expressions:

Character ranges can be specified using a variety of short expressions:

  • \w is same as [0-9 a-z A-Z]
  • \d is the equivalent to [0-9]
  • \s equals white space
  • \W anything which isn’t in  [0-9 a-z A-Z]
  • \D for values that are not a number
  • \S for values that are not a space

Example

a = "xyz1"
b = "xyz"
puts a =~ /\d/;
puts b =~ /\d/;

Output:

3
     #nil

!~ Operator

It’s just the opposite of =~ operator. In this operator, firstly, input goes or calls =~ operator if it returns an index. This operator returns false; if =~ operator returns nil, then this operator returns true as output.

Note: This operator only returns true or false.

Example

a = "xyz1"
puts a !~ /1/;
b = "xyz"
puts b !~ /1/;

Output:

false
true

 

Read about Bitwise Operators in C here.

Know more about Unary operator overloading in c++ in detail here.

Frequently Asked Questions

What is the difference between == and === operator?

‘==’ operator tests for equality of two values, whereas ‘===’ operator tests equality of a value in the given range.

What is the relation between the ‘=~’ and ‘!~’ operators?

‘!~’ operator works like the inverse of the ‘=~’ operator. It returns only boolean values based on the output of the ‘=~’ operator. If  ‘=~’ returns an index, then ‘!~’ returns false or true.

Is there an operator to perform the inverse of ‘===’ like ‘!==’?

No, there is no such operator. To perform negation on the ‘===’ operator, you have to do it manually.

Conclusion

This article on Ruby extensively discussed Ruby's equality operators, including ==, ===, !=, =~, and !~. Initially, we briefly introduced the operators, their syntax, how to use them, and then each of these operators, along with an example.

Refer to our guided paths on Coding Ninjas Studio to help yourself with concepts like Data Structures and AlgorithmsCompetitive ProgrammingSystem DesignJavaScript, and many more! Check out the mock test series and participate in the contests hosted on Coding Ninjas Studio to test your competency and coding skills. Also, if you have just begun your journey and are only looking for tech giants like Amazon, Uber, Microsoft, etc., in that case, you should look at the several problemsinterview experiences, and the interview bundles for placement preparations.

Do upvote our blogs if you find them helpful!

Live masterclass