Daniel Finnie
4/17/2008 2:21:00 AM
Hi,
Ruby substitutes the elements in the array for the percent signs in
the string. The "3d" and "04.2f" tell Ruby how to format those
numbers:
>> "The number is: %.2f" % 1.23456
=3D> "The number is: 1.23"
The documentation for sprintf has more info:
daniel@daniel-desktop:~/lightconsole$ ri sprintf
--------------------------------------------------------- Kernel#sprintf
format(format_string [, arguments...] ) =3D> string
sprintf(format_string [, arguments...] ) =3D> string
------------------------------------------------------------------------
Returns the string resulting from applying format_string to any
additional arguments. Within the format string, any characters
other than format sequences are copied to the result. A format
sequence consists of a percent sign, followed by optional flags,
width, and precision indicators, then terminated with a field type
character. The field type controls how the corresponding sprintf
argument is to be interpreted, while the flags modify that
interpretation. The field type characters are listed in the table
at the end of this section. The flag characters are:
Flag | Applies to | Meaning
---------+--------------+-----------------------------------------
space | bdeEfgGiouxX | Leave a space at the start of
| | positive numbers.
---------+--------------+-----------------------------------------
(digit)$ | all | Specifies the absolute argument number
| | for this field. Absolute and relative
| | argument numbers cannot be mixed in a
| | sprintf string.
---------+--------------+-----------------------------------------
# | beEfgGoxX | Use an alternative format. For the
| | conversions `o', `x', `X', and `b',
| | prefix the result with ``0'', ``0x'', ``0X=
'',
| | and ``0b'', respectively. For `e',
| | `E', `f', `g', and 'G', force a decimal
| | point to be added, even if no digits follo=
w.
| | For `g' and 'G', do not remove trailing ze=
ros.
---------+--------------+-----------------------------------------
+ | bdeEfgGiouxX | Add a leading plus sign to positive number=
s.
---------+--------------+-----------------------------------------
- | all | Left-justify the result of this conversion=