def int? : Int32?
2
end
case true
when x = int?
puts x # not reached
else
puts ":("
end
This:
case exp
when something
do_something
else
do_something_else
end
is rewritten to:
if something === exp
do_something
else
do_something_else
end
In your case, your code is equivalent to:
if (x = int?) === true
puts x
else
puts ":("
end
and 2 === true is false.
Everything is working as expected. This is not a bug.
You probably wanted to write:
# Note that there's no case expression
case
when x = int?
puts x
else
puts ":("
end
Most helpful comment
You probably wanted to write: