Jörg Abelshauser wrote:
> Arfon Smith 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 Pine 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



-- 
Posted via http://www.ruby-forum.com/.