Use of Case Equality operator (===)

Case Equality (===)

Also known as triple equals.

Mainly in ruby, every time we go through this tripe equals, we think of comparing the value but as a result, we don't get the expected result.

So the main purpose of this operator is, it does not test quality, but rather tests if the right operand has an IS-A Relationship with the left operand.

the best way to describe x === y is, does the set x include the member y?

Example to demonstrate the Case Equality

(2..6) === 5 # => true
(2..6) === 8 # => false


/pal/ === 'Nepal' # => true
/pal/ === 'Cool' # => false

Integer === 5      # => true
Integer === 'five' # => false

So with this example, I think you understood the case operator. There are many classes that override === to provide significant semantics in the case of the statements. Some of them are;

Array  || ==
Date   || ==
Module || is_a?
Object || ==
Range  || include?
Regxp  || =~
String || ==

Some of the examples of good practices.

#Bad Practise 
Integer === 5
(2..6) === 6
/pal/ === 'Nepal'

# Good Practise
5.is_a?(Integer)
(2..6).include?(6)
/pal/ =~ 'Nepal'

It is recommended that the case equality should be avoided. It doesn't test equality but rather includes and its use can be confusing. Code is more clear and easier to understand when the synonym method is used instead.