Try the following code on your ruby.
p('%-03d' % -5)
The result on ruby 1.8.*
is "-05"
while the result on ruby 1.9.*
is "-5 "
.
So, which behavior is correct? The answer is the latter.
String#%
is subject to be equivalent tosprintf(3)
"-"
means "left-align""0"
means "completing the spaces with 0" when the alignment is right-align
So, "-0"
is equivalent to mere "-"
. According to the principle, the behavior on ruby 1.8
is wrong.
(I think that the reason why ruby core developers don't change the 1.8's behavior is that the change may break existing codes.)
No comments:
Post a Comment