Explicit return from a Ruby method - implementation hiding, reuse and optimisations.

Posted by Chris McCauley on Stack Overflow See other posts from Stack Overflow or by Chris McCauley
Published on 2009-12-23T11:31:07Z Indexed on 2010/03/26 5:03 UTC
Read the original article Hit count: 478

Filed under:
|

Hi,

Rather than allowing a Ruby method to blindly return the last statement evaluated, is there any advantage to returning nil explicitly?

In terms of implementation hiding and reuse, it seems dangerous to blindly allow the last expression evaluated to be returned - isn't there a danger that a user will rely on this only to get a nasty surprise when the implementation is modified? Surely returning nil would be better unless an explicit return value was given.

Following on, is there an optimisation that Ruby can make when the return is a simple type rather than a reference to a more complex object?

Chris

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails