From: Sam Duncan Date: 2011-04-28T05:35:17+09:00 Subject: Re: Help me understand this SIMPLE Ruby code, using puts in a def 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 => nil ruby-1.9.2-p0 > foo => 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 > "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. > > Thanks >