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
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:

man printf

0 The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f,
F, g, and G conversions, the converted value is padded on the left with zeros
rather than blanks. If the 0 and - flags both appear, the 0 flag is ignored.
If a precision is given with a numeric conversion (d, i, o, u, x, and X), the
0 flag is ignored. For other conversions, the behavior is undefined.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

i think the pickaxe is simply incorrect - the printf function just delegates
to stdlib printf after all.

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?

agreed.

-a

···

On Thu, 13 Feb 2003, Warren Brown wrote:

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi,

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:

Pickaxe is great, but not perfect (nor me).

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?

This sounds nice. In fact, I have a plan to do so.

						matz.
···

In message “sprintf() bug” on 03/02/13, “Warren Brown” wkb@airmail.net writes: