Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The switch statement in Ruby uses the `===` method under the hood. `===` does not mean "equals" like it does in Javascript. Instead, `a === b` means, does `b` belong in the set of `a`.

    irb(main):001:0> Integer === Integer
    => false
    irb(main):002:0> String === String
    => false
    irb(main):003:0> Object === Integer
    => true
    irb(main):004:0> Object === String
    => true



Yeah: it's almost more true to say that `a === b` just means whatever we've decided `case b when a` should check. `===` is almost never used directly.


This makes more sense and seems less egregious when you look at it in use, in a case:

  a = []
  case a
  when Array
    puts true
  else
    puts false
  end


This might confuse a reader. The Object === cases are successful, because the Class-objects Integer and String are Objects. In ruby everything is an Object.


That's a good point. This might be a better example:

    irb(main):009:0> String === String
    => false
    irb(main):010:0> Integer === Integer
    => false
    irb(main):011:0> Class === Integer
    => true
    irb(main):012:0> Class === String
    => true




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: