On Mon, Apr 28, 2008 at 9:50 PM, Toki Toki <toki84 / gmail.com> wrote:
> Hi all!
>
>  I'm testing the functionality of sprintf and I know that I can use it in
>  two way:
>
>  sprintf("%.3f", "1.2345")
>  => "1.234"
>
>  Or
>
>  "%.3f" % "1.2345"
>  => "1.234"
>
>  In the second example, what does mean the percent symbol (%) between
>  "%.3f" and "1.2345"? It means apply format string "%.3f" to string
>  "1.2345"? What I want to know is, the percent symbol is limited to
>  sprintf or it has a more general use in ruby?

% is a clever trick to define operator % (as in modulo, i.e. 7 % 3 ==
1) on strings
to apply formatting. The same trick is used in python, and even in C++
(boost::format).
You'll find more details at http://ruby-doc.org/core/classes/String.html#M000785

>  I've searched on the ruby pickaxe and several other ruby books but I
>  haven't found anything on the % alone, obviously there are many use of
>  that combined with option (array: %w, string: %q,%Q, regexp: %r, ...).

This is another %.