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.

···

--
Posted via http://www.ruby-forum.com/.

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 class String - RDoc Documentation

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

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

···

On 28.04.2008 21:50, Toki Toki wrote:

Hi --

···

On Tue, 29 Apr 2008, Toki Toki 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?

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!

Robert Klemme 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.

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

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. /\/+/

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

···

On Tue, 29 Apr 2008, Robert Klemme wrote:

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

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.

···

--
Posted via http://www.ruby-forum.com/.

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

Kind regards

robert

···

2008/4/29 7stud -- <bbxx789_05ss@yahoo.com>:

Robert Klemme 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.

--
use.inject do |as, often| as.you_can - without end

> > 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. /\/+/
>

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

···

2008/4/29 David A. Black <dblack@rubypal.com>:

On Tue, 29 Apr 2008, Robert Klemme wrote:

--
use.inject do |as, often| as.you_can - without end