I wrote a piece of code that might be relevant to it. The report function for this code should return 0, but it returns 99, and the first puts call outputs a completely unrelated error value.
def report
need_forward = false
begin
snapshot = 99 if need_forward
rescue e : Exception
return
end
puts snapshot # Wrong value, for example: 1068599224
snapshot_id =
if snapshot
99
else
0
end
snapshot_id
end
puts report # => 99
The reason I found it is because I used return. But I still can’t understand this phenomenon.
Why is the type of snapshot not (Int32 | Nil) but Int32, why should it be filled with the wrong value when it is nil.
If I had to guess id vote it would have something to do with the fact its not being initialized. If you declare/initialize the variable to something else before the begin/rescue, it works as expected.