I know I’ve seen it somewhere in the doc but I can’t remember where…
BTW, why does the “if” works and the case/when doesn’t ?
a = "hello"
puts typeof(a).class
puts "got it" if typeof(a) == String
case typeof(a)
when Int32
puts "hello"
when String
puts "bozo"
else
puts "I dunno"
end
There’s an example of it in case - Crystal. Just do case a
without the typeof
.
String == String # => true
String === String # => false
Ultimately it’s because case
uses #===
and not #==
.
1 Like
Mmm still a problem : case/in is exhaustive, and I only want to check for some types
Use when
instead of in
like you did above.
case a
when String
# do string things
# `a` is constrained to `String` at compile time in this block
when Int32
# do int things
# `a` is constrained to `Int32` at compile time in this block
else # this is optional
# do non-string/int things — `a` is constrained to any of its types
# *except* String or Int32 here
end
1 Like