Because when the compiler sees:
@var = some_expression
the compiler will try to infer the type from some_expression
. In this case it’s this:
x || nil
Can we infer something from x
? Let’s look if it has a type restriction. Nope, it doesn’t have any. Can we infer something from nil
? Yes! It’s nil
so it must be of type Nil
. Well, Nil
or something else (x) that we don’t know the type of. So the end result is that we can only infer it to be Nil
.
It doesn’t matter what you pass to the constructor. The compiler doesn’t look at calls to infer the type of instance vars: just what you assign to @x
inside the constructor (or, well, any other method, but never calls to methods.)