From: Jim Weirich Date: 2002-04-18T08:53:50+09:00 Subject: Re: Ways methods return values >>>>> "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. -- -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)