5 ms·
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` b
by hnuser847 3y ago
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
- radiospiel 3y agoThis might confuse a reader. The Object === cases are successful, because the Class-objects Integer and String are Objects. In ruby everything is an Object.
- hnuser847 3y agoThat'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
- LanceH 3y agoThis 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
- burke 3y agoYeah: 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.
- deleted 3y ago[deleted]