Sprintf: what does mean the percent symbol?

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.

On Mon, Apr 28, 2008 at 9:50 PM, Toki T. [email protected] wrote:

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

On 28.04.2008 21:50, Toki T. 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”

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

Hi –

On Tue, 29 Apr 2008, Toki T. wrote:

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

Robert K. wrote:

it’s a float format and not a string format.

Or

“%.3f” % “1.2345”
=> “1.234”

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.

2008/4/29 7stud – [email protected]:

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

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

Kind regards

robert

Hi –

On Tue, 29 Apr 2008, Robert K. wrote:

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

But not the brain :slight_smile: I know there’s no consensus about these things,
but I’ll just put in a word for //. Having trained my brain to
understand //+/ at a glance, I always find it much harder to
understand things like {/+}, and similarly with /x-style regexes,
which I always have to slow down to read.

Chacun à son cerveau :slight_smile:

David

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.

2008/4/29 David A. Black [email protected]:

if you have a regular expression containing forward slashes the %r form is
much easier on the eye:

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

But not the brain :slight_smile: I know there’s no consensus about these things,

Yeah, my initial reaction was “maybe not for your brain”. :slight_smile:

but I’ll just put in a word for //. Having trained my brain to
understand //+/ at a glance, I always find it much harder to
understand things like {/+}, and similarly with /x-style regexes,
which I always have to slow down to read.

I for my part find longish regular expressions much, much more
readable and understandable if they are pulled apart with /x probably
across multiple lines and with comments.

Chacun à son cerveau :slight_smile:

I’m glad babelfish can help my brain decipher this. :-)))

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs