I just noticed that Array does not override the triple equal sign === method, which is sometimes called the case equality method.
x = 2 case x when [1, 2, 3] then "match" else "no match" end
whereas the range operator:
x = 2 case x when 1..3 then "match" else "no match" end
However, you can make a workaround for arrays:
x = 2 case x when *[1, 2, 3] then "match" else "no match" end
Is it known why this is so?
This is because it is more likely that x will be the actual array than the range, and overriding the array === means that regular equality will not match?
# This is ok, because x being 1..3 is a very unlikely event # But if this behavior occurred with arrays, chaos would ensue? x = 1..3 case x when 1..3 then "match" else "no match" end # => "no match"
source share