From: Warren Brown Date: 2003-02-13T03:25:45+09:00 Subject: sprintf() bug I have run across a discrepancy between the documentation and ruby 1.6.8. According to the pickaxe book, the flag character '0' changes padding from spaces (' ') to zeros ('0'). This works for numbers, but does not work for stings: irb(main):001:0> sprintf "%05d,%05s", 1, '1' => "00001, 1" A simple fix (from the 1.6.8 version) would be: -------------------------------- --- sprintf.c.old Thu Dec 12 03:16:44 2002 +++ sprintf.c.new Wed Feb 12 11:12:07 2003 @@ -287,8 +287,9 @@ CHECK(width); width -= len; if (!(flags&FMINUS)) { + char c = (flags & FZERO) ? '0' : ' '; while (width--) { - buf[blen++] = ' '; + buf[blen++] = c; } } -------------------------------- To get around this problem, I ended up using the combination mystring.rjust(5).tr(' ','0'). This worked for me since my string was guaranteed not to have any spaces, but if that were not the case, this would get messy. It occurred to me that String#ljust and String#rjust really seem like they should have an optional second parameter that defaults to ' ', but could be any character (or any string?). Anyone else think this is a good idea? - Warren Brown