Sprintf percent sign problem

Hi,
I am trying to format some text using sprintf. The text itself
contains the percent sign (%).

When I use it without any escape character, as expected ruby throws
error (unkonown format sequence).

When I use the “%” sequence ruby complaints when the “%” is
somewhere in the text. When it is at the end it works but I don’t want
it there.

When I use the “%%” sequence, I get the exact sequence as output ("%%"
is printed!!)… \

Has anyone observed this behavior? Am I doing something wrong or it is
a ruby bug?

Thanks in advance,
Efthimios.

Efthimios Bothos wrote:

Hi,
I am trying to format some text using sprintf. The text itself
contains the percent sign (%).

When I use it without any escape character, as expected ruby throws
error (unkonown format sequence).

I’m not seeing that:

str = “hello%world”
new_str = sprintf(’%20s’, str)
puts new_str

–output:–
hello%world

It’s always baffling to me why a poster doesn’t just post a simplified
example along with the error.

It’s always baffling to me why a poster doesn’t just post a simplified
example along with the error.

If they knew to provide tests, they wouldn’t be asking syntax
questions in the first place. That’s an experienced programmer’s rule,
and syntax questions usually come from newbies.

It is a very good idea to provide test cases in responses, though.
Apart from anything else, whenever I do that I go into IRB and half
the time find out something more interesting than whatever I was going
to say.


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/