From: dblack@... Date: 2007-02-19T22:43:07+09:00 Subject: Re: return statement Hi -- On Mon, 19 Feb 2007, David Vallner wrote: > Making a wild guess at the signal in the noise. > > On Mon, 19 Feb 2007 13:44:46 +0100, Derek Teixeira > wrote: >> Along with that confusion on the return function.. i was code tracing >> the code below and i understand it, except for one part, i just don't >> see where the code tells the program to print out the numString. I see >> it telling the program to in the 'one hundred' section, but not at all >> in the teens or single digits? >> >> [snip self-depreciation - just keep that one out next time ;P] >> >> def englishNumber number >> ## [snip unrelated code] >> >> # If we got this far, then we had a number somewhere >> # in between 0 and 100, so we need to return "numString". >> numString ## equivalent to "return numString" >> end >> > > In Ruby, a function always returns the value of the last expression in its > definition by default if it reaches the end. > > (I really, really, really hate on relying on this behaviour because it makes > it easy to miss the fact that this is happening instead of just a "void" > method call, or to miss that the method is in fact returning anything at all, > and it really doesn't belong in a beginners' book. Tut. Oversight maybe?) No; Chris knows what he's doing. It's pretty fundamental -- I always make a point of explaining it when I introduce the topic of methods to beginners. Otherwise simple things like: def of_age? age > 17 end make no sense. It's basically just a function that performs a calculation. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)