Bug in sprintf?

H

Hal Fulton

Carlos said:
For that conversion, #to_i could have been used as well, and then '09' would
be interpreted as decimal 9. Why should I expect my string to be passed
through Integer() before formatting? That is not documented.

Why would you pass a string to something that expects a number
anyway?


Hal
 
H

Hal Fulton

Hal said:
Why would you pass a string to something that expects a number
anyway?

Actually, let me say this, as it's more informative and
constructive.

If you need to convert your string to a number, it's best
to do so explicitly. (If you really want to pass a string
into sprintf, %s is the correct modifier.)

Also note that while Integer() doesn't convert the way
you want, to_i does:

Integer("09") # error
"09".to_i # 9


Cheers,
Hal
 
H

Hal Fulton

Robert said:
Hal
all you are saying is true BUT
I do not think we should blame C for something which really is not nice.
I know not many share my opinion but the brave speaks out nevertheless ;)

IMHO the interpreter should raise an expression when a String is passed to
%d (as you asked OP rightfully not to do so).
Early Failure, please, *early*.

I see your point. Truthfully, I think automatic conversion of a
string in this case is probably a bad idea. And that is a Ruby
issue, not a C one.
BTW who is talking about sprintf I am talking about "%" a very rubish
construct.

But format % array behaves the same as sprintf(format,*array)

The specifiers are the same (and the same as C, perhaps with a few
minor differences).


Hal
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top