Reputation: 107728
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
Upvotes: 2
Views: 335
Reputation: 799110
The 1.8 documentation says that %u
is 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 %u
is the same as %d
, so that behavior is correct.
Upvotes: 5