On Thu, 13 Feb 2003, Warren Brown wrote:

>     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

-- 

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