Confusion Over Triple Equals (===)

Doesn’t everyone have concepts that they can’t keep organized in their heads? I frequently get values for certain keys mixed up in my head, especially ones that I don’t use very often. At the Lone Star Ruby ConferenceGreg and I were talking about the triple equals operator in Ruby recently (===), and I confused my notion of it in Ruby with what I thought was its function in Perl. Obviously I don’t use it very much. Hopefully I can clear this up though.

In Perl, there is no triple equals operator, based on the perlop Perl operator documentation. I was really mixing things up there.

In PHP, the triple equals operator is used to test whether two things have the same value and type, according to the PHP Manual.

<?php
    $a = "1";
    $b = 1;

    if ($a == $b) {
      print "$a == $b";
    }

    if ($a === $b) {
      print "$a === $b";
    }

?>

This code prints

1 == 1

In Ruby, triple equals (Object#===) is, “effectively the same as calling #==, but typically overridden by descendants to provide meaningful semantics in case statements,” based on the Object class documentation. So, for classes like Array, #=== is effectively the same as #==. I say effectively since I haven’t actually perused the source of array.c, though you can see in object.c that rb_equal (===) calls == and then checks id_eq if that doesn’t return true. And in the case of your own objects, triple equals can be used to provide your own equality tests for case statements.

Reply