# Puts returns nil?

**URL:** <https://www.ruby-forum.com/t/puts-returns-nil/81160>\
**Category:** Ruby\
**Created:** [January 5, 2007, 10:30am UTC](https://www.ruby-forum.com/t/puts-returns-nil/81160 "2007-01-05T10:30:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arfon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@arfon](https://www.ruby-forum.com/u/arfon)\
**Post date:** [January 5, 2007, 10:30am UTC](https://www.ruby-forum.com/t/puts-returns-nil/81160/1 "2007-01-05T10:30:19Z")

</div>

Hi, I’m sorry to be asking such a daft question but I need a concept  
explaining…

I’m working through the excellent Chris P. book ‘Learn to program’ and  
I don’t get one of the examples he gives. In particular I don’t follow  
the following example:

def say\_moo number\_of\_moos  
puts ‘mooooooo…’ \* number\_of\_moos  
‘yellow submarine’  
end

x = say\_moo 3  
puts x.capitalize + ‘, dude…’  
puts x + ‘.’

Now the output from this is:

mooooooo…mooooooo…mooooooo  
Yellow submarine, dude…  
yellow submarine

* * *

OK, so here’s the problem, I _don’t_ get why it doesn’t return the  
following:

mooooooo…mooooooo…mooooooo  
Yellow submarine, dude…  
mooooooo…mooooooo…mooooooo  
yellow submarine

* * *

Why do we get one set of ‘moos’ but not a second?

Sorry for such a newbiee question.

Cheers

arfon

---

<div class="post-metadata">

**Author:** ![arfon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@arfon](https://www.ruby-forum.com/u/arfon)\
**Post date:** [January 5, 2007, 10:53am UTC](https://www.ruby-forum.com/t/puts-returns-nil/81160/2 "2007-01-05T10:53:06Z")

</div>

Arfon S. wrote:

> Hi, I’m sorry to be asking such a daft question but I need a concept  
> explaining…
> 
> I’m working through the excellent Chris P. book ‘Learn to program’ and  
> I don’t get one of the examples he gives. In particular I don’t follow  
> the following example:
> 
> def say\_moo number\_of\_moos  
> puts ‘mooooooo…’ \* number\_of\_moos  
> ‘yellow submarine’  
> end
> 
> x = say\_moo 3  
> puts x.capitalize + ‘, dude…’  
> puts x + ‘.’
> 
> Now the output from this is:
> 
> mooooooo…mooooooo…mooooooo  
> Yellow submarine, dude…  
> yellow submarine
> 
> * * *
> 
> OK, so here’s the problem, I _don’t_ get why it doesn’t return the  
> following:
> 
> mooooooo…mooooooo…mooooooo  
> Yellow submarine, dude…  
> mooooooo…mooooooo…mooooooo  
> yellow submarine
> 
> * * *
> 
> Why do we get one set of ‘moos’ but not a second?
> 
> Sorry for such a newbiee question.
> 
> Cheers
> 
> arfon

Hi Arfon  
in every ruby-function, the last statement (here ‘yellow submarine’) is  
the return-value of this function, nothing else !  
Further yes, puts returns nil.  
puts(obj, …) =\> nil  
You can see the declaration of all ruby-functions in the “fxri”,  
installed with your ruby-interpreter. It’s very helpful to work with it.  
3. If you intention was to append the ‘yellow submarine’ to the puts 1  
line above, you need an ‘+’ to do this:

puts ‘mooooooo…’ \* 3 + ‘yellow submarine’  
mooooooo…mooooooo…mooooooo…yellow submarine

best regards JÃ¶rg

---

<div class="post-metadata">

**Author:** ![arfon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@arfon](https://www.ruby-forum.com/u/arfon)\
**Post date:** [January 5, 2007, 11:05am UTC](https://www.ruby-forum.com/t/puts-returns-nil/81160/3 "2007-01-05T11:05:26Z")

</div>

JÃ¶rg Abelshauser wrote:

> Arfon S. wrote:
> 
> > Hi, I’m sorry to be asking such a daft question but I need a concept  
> > explaining…
> > 
> > I’m working through the excellent Chris P. book ‘Learn to program’ and  
> > I don’t get one of the examples he gives. In particular I don’t follow  
> > the following example:
> > 
> > def say\_moo number\_of\_moos  
> > puts ‘mooooooo…’ \* number\_of\_moos  
> > ‘yellow submarine’  
> > end
> > 
> > x = say\_moo 3  
> > puts x.capitalize + ‘, dude…’  
> > puts x + ‘.’
> > 
> > Now the output from this is:
> > 
> > mooooooo…mooooooo…mooooooo  
> > Yellow submarine, dude…  
> > yellow submarine
> > 
> > * * *
> > 
> > OK, so here’s the problem, I _don’t_ get why it doesn’t return the  
> > following:
> > 
> > mooooooo…mooooooo…mooooooo  
> > Yellow submarine, dude…  
> > mooooooo…mooooooo…mooooooo  
> > yellow submarine
> > 
> > * * *
> > 
> > Why do we get one set of ‘moos’ but not a second?
> > 
> > Sorry for such a newbiee question.
> > 
> > Cheers
> > 
> > arfon
> 
> Hi Arfon  
> in every ruby-function, the last statement (here ‘yellow submarine’) is  
> the return-value of this function, nothing else !  
> Further yes, puts returns nil.  
> puts(obj, …) =\> nil  
> You can see the declaration of all ruby-functions in the “fxri”,  
> installed with your ruby-interpreter. It’s very helpful to work with it.  
> 3. If you intention was to append the ‘yellow submarine’ to the puts 1  
> line above, you need an ‘+’ to do this:
> 
> puts ‘mooooooo…’ \* 3 + ‘yellow submarine’  
> mooooooo…mooooooo…mooooooo…yellow submarine
> 
> best regards JÃ¶rg

Ahh, OK, thanks for this. I think I was confused as I thought the first  
‘puts’, i.e.  
puts x.capitalize + ‘, dude…’

was producing the moos and the second one (puts x + ‘.’) wasn’t. Both  
of the puts are just returning the last statement, it’s the ‘x= say\_moo  
3’ that is giving the moooo…mooooo…moooooo…

Right?!

Cheers  
arfon

---

<div class="post-metadata">

**Author:** ![arfon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@arfon](https://www.ruby-forum.com/u/arfon)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/puts-returns-nil/81160/4 "2007-01-06T19:30:42Z")

</div>

Arfon S. wrote:

> […]

A method returns the value of the last statement, not a list of all of  
them. In your method, the return value of puts (which is nil, btw) is  
discarded.

> def say\_moo number\_of\_moos  
> puts ‘mooooooo…’ \* number\_of\_moos  
> ‘yellow submarine’  
> end
> 
> x = say\_moo 3

Here you call say\_moo. Say\_moo puts 3 moo’s in you screen, then returns  
‘yellow submarine’. This return value is assigned to x.

> puts x.capitalize + ‘, dude…’

Puts the value of x (‘yellow submarine’) capitalized, plus ‘, dude…’.

> puts x + ‘.’

Puts the value of x, plus a dot.

HTH. Good luck.

---

<div class="post-metadata">

**Author:** ![arfon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@arfon](https://www.ruby-forum.com/u/arfon)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/puts-returns-nil/81160/5 "2007-01-06T19:30:52Z")

</div>

Alle 10:30, venerdÃ¬ 5 gennaio 2007, Arfon S. ha scritto:

> puts ‘mooooooo…’ \* number\_of\_moos  
> Yellow submarine, dude…  
> mooooooo…mooooooo…mooooooo  
> arfon  
> say\_moo does two separate things:  
> 1- writes the string ‘mooooooo…’ on the screen number\_of\_moos times  
> 2- returns the string ‘yellow submarine’  
> The call to puts in the definition of say\_moo has nothing to do with  
> say\_moo’s  
> return value, which is determinated by the method’s last line: ‘yellow  
> submarine’.

Writing x=say\_moo 3 will put in x only the string ‘yellow submarine’,  
because  
this is what say\_moo returns. In other words, the line  
mooooooo…mooooooo…mooooooo is written by a side effect of say\_moo;  
the  
other two lines are produced by your own puts statements.

To get the output you expected (well, not exactly, captialize would work  
a  
little differently), say\_moo should have defined like this:

def say\_moo number\_of\_moos  
mooooooo…’ \* number\_of\_moos+"\nyellow submarine"  
end

In this case, say\_moo wouldn’t write nothing on the screen by itself,  
but  
would return the string  
“mooooooo…mooooooo…mooooooo  
yellow submarine”  
which would be written twice by your puts statements

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![arfon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@arfon](https://www.ruby-forum.com/u/arfon)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/puts-returns-nil/81160/6 "2007-01-06T19:30:35Z")

</div>

because you only call say\_moo once.

x = say\_moo 3

the interpreter will resolve and execute the method call and will  
store the _result_ in x thus say\_moo is called once and x is set to  
the string “Yellow submarine”

puts x

this will only print the content of x which is a string and won’t call  
say\_moo another time

did it help ?

jean
