At the bottom of Kernel#sprintf‘s documentation it has this example:
sprintf("%u", -123) #=> "..4294967173"
When I do this on 1.8.7 I get this result:
"..18446744073709551493"
That’s similar to the expected output, but is definitely not it.
When I do it on 1.9.2 however, I get the same number back as a string:
ruby-1.9.2-p136 :001 > sprintf("%u", -123)
=> "-123"
So there’s actually two questions here.
-
Why would I be getting a different output to what the documentation says I would and
- Why would Ruby 1.9.2 behave differently to 1.8.7, even when the documentation is quite specific about the output type?
The 1.8 documentation says that
%uis for a 32-bit unsigned integer, and you’re seeing it on a 64-bit unsigned integer, so that behavior is off. The 1.9 documentation says that%uis the same as%d, so that behavior is correct.