> Note: There're no break statements at the end of each when clause. Unlike other languages, Ruby's case doesn't fall through.
Aw no fall through?
> Note: Do not use obj.class in the case clause, as Integer === Integer returns false.
Wait what
> Note: There're no break statements at the end of each when clause. Unlike other languages, Ruby's case doesn't fall through.
Aw no fall through?
> Note: Do not use obj.class in the case clause, as Integer === Integer returns false.
Wait what
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 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 a = []
case a
when Array
puts true
else
puts false
endRuby's triple equals (`===`) is defined differently based on the type of the operand on the left hand side.
`a === b` is syntactic sugar for `a.===(b)`; and for classes `klass.===(other)` is defined as `other.is_a?(klass)`.
More examples here: https://dev.to/baweaver/understanding-ruby-triple-equals-2p9...
That's a good thing. In fact, that's the only thing on the article that is unambiguously good.
Fall through only serves to create bugs.
`Integer ===` (and more generally if `===` is implemented as a class method) checks if the right hand side is of the class Integer. The object Integer is not, it's an object of class Class.
EDIT: In other words:
$ pry
[1] pry(main)> Integer === Integer
=> false
[2] pry(main)> Class === Integer
=> true
[3] pry(main)> Integer.class
=> Classwhen 60, 70