From: "Jörg Abelshauser" Date: 2007-01-05T18:53:09+09:00 Subject: Re: puts returns nil? 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 -- Posted via http://www.ruby-forum.com/.