Is it always the norm to skip 'return'?

The only time you need to implicitly use 'return' is when you want the
value of a variable that was processed prior to the last statement.

for example:

   y = some_method(alpha, beta)

   do_some_cleanup before returning

   return y

···

On Oct 19, 2:10 pm, Greg Donald <g...@cyberfusionconsulting.com> wrote:

On Sat, 20 Oct 2007, Greg Donald wrote:
> On Sat, 20 Oct 2007, Andreas S. wrote:
> > But I think it's still a good idea to use return in order to document
> > that the method is MEANT to return something, and that it's not just an
> > unintended side effect.

> If you want to use return, use it. The value returned by a Ruby method
> is t

What I mean to say is the returned value is always going to be the value
of the last evaluated expression. Not having to use return is good.
It's smaller code and once you get used to it, it seems very normal.

--
Greg Donald
Cyberfusion Consultinghttp://cyberfusionconsulting.com/

Bernard Kenik wrote:

The only time you need to implicitly use 'return' is when you want the
value of a variable that was processed prior to the last statement.

for example:

   y = some_method(alpha, beta)

   do_some_cleanup before returning

   return y

You could also just write y without the return.

···

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

Actually, I'd say the only reason you should use return in a method is when you want to jump out of a method with a value and stop doing anything else, i.e.

def foo(arg)
   arg.each do |e|
     if e.meets_some_condition?
       return e.text
     end
   end

   "Not found"
end

The advantage of only using "return" when you want to return prematurely is that it clues people into the fact that you're exiting prematurely. Otherwise you know that the method always returns the value of the last expression you evaluate.

Ben

···

On Oct 19, 2007, at 15:05, bbiker wrote:

The only time you need to implicitly use 'return' is when you want the
value of a variable that was processed prior to the last statement.

for example:

   y = some_method(alpha, beta)

   do_some_cleanup before returning

   return y

Hi Ben and everyone who has replied,

Makes sense. Thanks for the input. I understand better now when the usage of 'return'.

I appreciate your help.

Regards,

-- Tito

···

On Oct 19, 2007, at 7:27 PM, Ben Giddings wrote:

On Oct 19, 2007, at 15:05, bbiker wrote:

The only time you need to implicitly use 'return' is when you want the
value of a variable that was processed prior to the last statement.

for example:

  y = some_method(alpha, beta)

  do_some_cleanup before returning

  return y

Actually, I'd say the only reason you should use return in a method is when you want to jump out of a method with a value and stop doing anything else, i.e.

def foo(arg)
arg.each do |e|
   if e.meets_some_condition?
     return e.text
   end
end

"Not found"
end

The advantage of only using "return" when you want to return prematurely is that it clues people into the fact that you're exiting prematurely. Otherwise you know that the method always returns the value of the last expression you evaluate.

Ben