printf goober

L

Lloyd Linklater

Hi there!

I was experimenting with printf thus:

puts printf("%s is %d years old.", "booboo", 12)

and got this result:

booboo is 12 years old.nil

Where does that nil originate and how do I get rid of it?
 
E

Eleanor McHugh

I was experimenting with printf thus:

puts printf("%s is %d years old.", "booboo", 12)

and got this result:

booboo is 12 years old.nil

Where does that nil originate and how do I get rid of it?


Your code contains two expressions:

puts (printf("%s is %d years old.", "booboo", 12))

so the printf is outputting the string "Booboo is 12 years old." and
returning the value nil,
then puts is outputting that nil


Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net
 
K

Kyle Schmitt

perhaps you're thinking of sprintf?

printf returns nil, but displays the string to the screen, so what's
happening here is
puts(printf("blah."))

printf prints out "blah." without a return, then puts prints out the
return value of printf, which is nil.

so you get

blah.nil


To do what you want, use sprintf, which returns for the formatted string

--Kyle
 
L

Lloyd Linklater

It turns out that I mixed the

printf("%s is %d years old.", "booboo", 12)

with

puts "%s is %d years old." % ["booboo", 12]

doh!
 

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
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top