# Help me understand this SIMPLE Ruby code, using puts in a def

**URL:** <https://rubytalk.org/t/help-me-understand-this-simple-ruby-code-using-puts-in-a-def/62556>\
**Category:** ruby-talk\
**Created:** [27 April 2011 20:25 UTC](https://rubytalk.org/t/help-me-understand-this-simple-ruby-code-using-puts-in-a-def/62556 "2011-04-27T20:25:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fily\_Salas](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@Fily\_Salas](https://rubytalk.org/u/Fily_Salas)\
**Post date:** [27 April 2011 20:25 UTC](https://rubytalk.org/t/help-me-understand-this-simple-ruby-code-using-puts-in-a-def/62556/1 "2011-04-27T20:25:48Z")

</div>

Hi,

I was practicing ruby and accidentally came across something that got me  
thinking (nothing important but I want to know why).

Why does the example-1 works and not example-2, if all I'm doing is  
moving the puts?

Example-1:  
def message  
&nbsp;&nbsp;"this is working"  
end  
puts message.upcase

Example-2:  
def message  
&nbsp;&nbsp;puts "this is working"  
end  
message.upcase

Can someone explain this a little bit? I could just ignore it but I  
would like to know the reason.

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sam\_Duncan](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Sam\_Duncan](https://rubytalk.org/u/Sam_Duncan)\
**Post date:** [27 April 2011 20:35 UTC](https://rubytalk.org/t/help-me-understand-this-simple-ruby-code-using-puts-in-a-def/62556/2 "2011-04-27T20:35:17Z")

</div>

This is where irb is handy. The short of it is that in your first example a string is returned on which you call upcase. In your second example the thing returned is the result of puts, which is nil.

\> irb  
ruby-1.9.2-p0 \> foo = puts 'foo'  
foo  
&nbsp;&nbsp;=\> nil  
ruby-1.9.2-p0 \> foo  
&nbsp;&nbsp;=\> nil  
ruby-1.9.2-p0 \> foo.upcase  
NoMethodError: undefined method `upcase' for nil:NilClass

Does that help?

Sam

> **···**
>
> On 28/04/11 08:25, Fily Salas wrote:
> 
> > Hi,
> > 
> > I was practicing ruby and accidentally came across something that got me  
> > thinking (nothing important but I want to know why).
> > 
> > Why does the example-1 works and not example-2, if all I'm doing is  
> > moving the puts?
> > 
> > Example-1:  
> > def message  
> > &nbsp;&nbsp;&nbsp;"this is working"  
> > end  
> > puts message.upcase
> > 
> > Example-2:  
> > def message  
> > &nbsp;&nbsp;&nbsp;puts "this is working"  
> > end  
> > message.upcase
> > 
> > Can someone explain this a little bit? I could just ignore it but I  
> > would like to know the reason.
> > 
> > Thanks

---

<div class="post-metadata">

**Author:** ![Roger\_Braun1](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@Roger\_Braun1](https://rubytalk.org/u/Roger_Braun1)\
**Post date:** [27 April 2011 20:41 UTC](https://rubytalk.org/t/help-me-understand-this-simple-ruby-code-using-puts-in-a-def/62556/3 "2011-04-27T20:41:49Z")

</div>

Hi Fily

> **···**
>
> 2011/4/27 Fily Salas \<fs\_tigre@hotmail.com\>:
> 
> > Hi,
> > 
> > I was practicing ruby and accidentally came across something that got me  
> > thinking (nothing important but I want to know why).
> > 
> > Why does the example-1 works and not example-2, if all I'm doing is  
> > moving the puts?
> > 
> > Example-1:  
> > def message  
> > "this is working"  
> > end  
> > puts message.upcase
> > 
> > Example-2:  
> > def message  
> > puts "this is working"  
> > end  
> > message.upcase
> > 
> > Can someone explain this a little bit? I could just ignore it but I  
> > would like to know the reason.
> 
> The return value of the "puts" method is always nil, not the string it  
> prints. What your second example does is try to invoke the "upcase"  
> method on nil.
> 
> --  
> Roger Braun  
> rbraun.net | humoralpathologie.de

---

<div class="post-metadata">

**Author:** ![Cee\_Joe](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@Cee\_Joe](https://rubytalk.org/u/Cee_Joe)\
**Post date:** [27 April 2011 20:47 UTC](https://rubytalk.org/t/help-me-understand-this-simple-ruby-code-using-puts-in-a-def/62556/4 "2011-04-27T20:47:26Z")

</div>

I agree with Sam:

> > def message  
> > "this is working"  
> > end

=\> nil

> > puts message.upcase

THIS IS WORKING  
=\> nil

> > def message1  
> > puts "this is working"  
> > end

=\> nil

> > puts message1

this is working  
nil \<----- extra nil  
=\> nil

Since the nil is the last thing that is read in the method, upcase is  
called on that nil and upcase throws an error b/c you can't use that  
method on a nil. Make sense?

-Cee

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Fily\_Salas](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@Fily\_Salas](https://rubytalk.org/u/Fily_Salas)\
**Post date:** [27 April 2011 23:38 UTC](https://rubytalk.org/t/help-me-understand-this-simple-ruby-code-using-puts-in-a-def/62556/5 "2011-04-27T23:38:09Z")

</div>

Got it. Thank you all very much for your help!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
