From: Sean O'Dell Date: 2002-04-18T09:46:19+09:00 Subject: Re: Ways methods return values "Jim Weirich" wrote in message news:87g01tdgpd.fsf@traken.access.one.net... > >>>>> "Sean" == Sean O'Dell writes: > > Sean> I know in Ruby, the last evaluation is returned by a method > Sean> and you can also explicitly "return" a value, but I was > Sean> wondering if there was a way to do something I became > Sean> accustomed to while programming in Pascal. > > I would like to suggest an alternate style that you might find > readable and is easier to map into Ruby. > > The Eiffel language also uses assignment to a (seemingly) local > variable to indicate the return value of a function. But instead of > using the function name, the Eiffel variable is *always* named > "Result". This makes post-conditions read very nicely, e.g. you could > assert things like "Result > 0" (pre and post conditions are a big > part of the Eiffel language). > > In Ruby, I've gotten in the habit of using a variable named "result" > to hold the return type of a function. The name of the variable makes > its intent clear to readers of the code. The only thing I have to > remember is to make "result" the last line of my function. > > For example, a factorial function would look like this: > > def factorial(n) > result = 1 > (2..n).each { |i| result *= i } > result > end > > Its just a suggestion. Pascal actually does the same thing and I usually find "result" much more modular than using the function name itself. This is definitely something I would like to see in Ruby. I'm thinking if a method never uses the result variable, then it can still return the last evaluation so as not to break most existing scripts. Scripts would break though...surely people use the variable "result" and still expect the last evaluation to be returned from their methods. It would be a sweet addition though; I'm really missing it right now. Sean