# Printf goober

**URL:** <https://www.ruby-forum.com/t/printf-goober/141611>\
**Category:** Ruby\
**Created:** [June 27, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/printf-goober/141611 "2008-06-27T17:54:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [June 27, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/printf-goober/141611/1 "2008-06-27T17:54:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [June 27, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/printf-goober/141611/2 "2008-06-27T18:01:27Z")

</div>

On 27 Jun 2008, at 16:52, Lloyd L. 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-brains.net](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [June 27, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/printf-goober/141611/3 "2008-06-27T18:02:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [June 27, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/printf-goober/141611/4 "2008-06-27T20:12:41Z")

</div>

Perfect! Thanks!

---

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [June 27, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/printf-goober/141611/5 "2008-06-27T20:52:04Z")

</div>

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!
