sprintf percent sign problem

M

mpthimios

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.
 
B

bbxx789_05ss

Efthimios said:
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.
 
G

gilesb

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 Bowkett

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

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

Similar Threads

Sprintf: what does mean the percent symbol? 7
Open-URI and percent sign in url 2
Displaying a "+" sign 5
sprintf 15
sprintf a filename 7
Named sprintf parameters 14
sprintf 66
Passing percent sign in querystring 11

Members online

Forum statistics

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

Latest Threads

Top