Triple equals in Ruby

Tram Ho

What is Triple equals?

By default it is the alias of the == comparison operator with the use of equal comparison of values ​​and data types. It is most evident when classes override their own behavior. Let’s see some examples of it:


For Ranges, === is an alias for includes? , it helps you check if the value is in a given ranges. More specifically, it doesn’t just check numbers, with a range of String that is still checkable.

Regular Expressions

As for Regex, === is a secret for the match? , for a given Regex it checks to see if the string to be compared is a child of the Regex result set.


For Class, === is a secret for is_a? , check if the given value belongs to this class or not?

Functions (Proc and Lambda)

Ruby has several ways to represent anonymous functions, procs, and lambdas. They can be expressed as follows:

Here === is similar to the call method. Proc and Lambda s will be called with params as object to be executed. The block will be run and return results corresponding to the value you passed.

IP Addresses

Ruby also has features for all of us operations and network types, IPAddr:

You can instantiate subnets using it:

Use === instead of include? as here:

Here we are checking if the IP address is a member of a certain subnet or not


There are many interesting things about the === comparison operator, here I only introduce a few, you can learn more at the link below.

Share the news now

Source : Viblo