2

I was testing a snippet of code, then I found this:

2.1.2 :016 > 1 === (1..12)
 => false
2.1.2 :017 > (1..12) === 1
 => true
2.1.2 :018 >

Why does the order matter in this boolean expression?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
yeyo
  • 2,954
  • 2
  • 29
  • 40
  • `===` is just a garden-variety method with an unusual name. For a given method `m`, would you expect `1.m(1..12) == (1..12).m(1)`? No. – Cary Swoveland Oct 26 '14 at 23:17
  • 6
    Consider this, `===` is saying "is the object on the right an instance of the class on the left and/or contained by it?". So, is `1` an instance of `1..12` and/or contained by it? Yes. Is `1..12` an instance or contained by `1`? No. – the Tin Man Oct 26 '14 at 23:19
  • @CarySwoveland `==` is also a method. – yeyo Oct 26 '14 at 23:21
  • 3
    Some methods are symmetric, most are not. The only reason you think `===` (as opposed to `some_method`) should be symmetric is because it resembles the method `==`, which is. Close only counts in horseshoes. – Cary Swoveland Oct 26 '14 at 23:31
  • 1
    `===` is just a method of an object on the left side and object on the right side is an argument. In the first case it's `Fixnum#===` ie. whether argument equals numerically whereas the second case is `Range#===` which returns true if argument is an element of the range. So you are comparing apples & oranges here. – David Unric Oct 27 '14 at 01:47

0 Answers0