[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

printf goober

Lloyd Linklater

6/27/2008 3:52:00 PM

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?
--
Posted via http://www.ruby-....

4 Answers

Eleanor McHugh

6/27/2008 3:59:00 PM

0

On 27 Jun 2008, at 16:52, Lloyd Linklater wrote:
> 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-...
----
raise ArgumentError unless @reality.responds_to? :reason



Kyle Schmitt

6/27/2008 3:59:00 PM

0

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
On Fri, Jun 27, 2008 at 10:52 AM, Lloyd Linklater <lloyd@2live4.com> wrote:
> 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?
> --
> Posted via http://www.ruby-....
>
>

Lloyd Linklater

6/27/2008 6:10:00 PM

0

Perfect! Thanks!
--
Posted via http://www.ruby-....

Lloyd Linklater

6/27/2008 6:50:00 PM

0

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!
--
Posted via http://www.ruby-....