Sprintf: what does mean the percent symbol?

T

Toki Toki

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?

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, ...).

Thanks.

Best regards.
 
R

Robert Klemme

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"

Although it works the way you did it, that should have read

sprintf("%.3f", 1.2345)

IOW, it's a float format and not a string format.
Or

"%.3f" % "1.2345"
=> "1.234"

Same here:

"%.3f" % 1.2345

Actually you can do

irb(main):001:0> "%3d %04d" % [1,2]
=> " 1 0002"

i.e. work with multiple arguments. But for that I prefer (s)printf.
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 an operator and is implemented / overloaded for several classes.
The most commonly used is probably modulus operation for Fixnums.

irb(main):003:0> 10 % 3
=> 1
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, ...).

That's a completely different story, here % I would not call "operator"
because it is not runtime effective. This occurrence of the percent
sign is evaluated at compile time, more precisely during parsing. All
these are convenience syntaxes to more easily express certain literals.
For example, if you have a regular expression containing forward
slashes the %r form is much easier on the eye:

%r{/+} vs. /\/+/

Kind regards

robert
 
J

Jano Svitok

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 %.
 
D

David A. Black

Hi --

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?

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, ...).

You can define a % method, which will then be executed when you use
the infix %.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!
 
7

7stud --

Robert said:
it's a float format and not a string format.


Same here:

"%.3f" % 1.2345

According to pickaxe2, p. 606, "%" is the name of an instance method in
the String class, and its syntax is:

str % arg

The return value is a string.
 
R

Robert Klemme

2008/4/29 7stud -- said:
According to pickaxe2, p. 606, "%" is the name of an instance method in
the String class, and its syntax is:

str % arg

The return value is a string.

I'm not sure I get your point. Can you elaborate?

Kind regards

robert
 
T

Toki Toki

Thanks to all for the help and fast answers.

So back to my two example:

sprintf("%.3f", "1.2345")
=> "1.234"

Or

"%.3f" % "1.2345"
=> "1.234"

Are they the same thing written in different way or the first use
sprintf and the second use str % arg, both giving the same result?

Thanks.

Best regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top