I have a Ruby code as shown below, Try it online!
loop{p$.+=1r}
and another one, in which I just divide the 1r by 2 as shown below, Try it online!
loop{p$.+=1r/2}
As far as I can guess, the type-casting isn't working properly with $.. In the first case, it works correctly, but in the second one, it doesn't. strange!
So, why it works that way? Or, maybe my guess is wrong, then what is the actual issue, and how to resolve it?
Usually, Integer are up-casted to more complex data types, like in this code, it seems to work.
it works correctly, type of the
$.variable isIntegerSo right side of the addition will be casted to the
Integer.In first case
1ris casted to theIntegerof value1, so you can see value is changing.In second case
1r/2is casted to the Integer of value0, so you don't see changes because adding zero will not change original value.You can not change type of pre-defined global variable, just try
$. = "text"