Coming from a Python background, where there is always a \"right way to do it\" (a \"Pythonic\" way) when it comes to style, I\'m wondering if the same exists for Ruby. I\'v
Actually the important thing is to distinguish between:
Ruby does not have a native way of distinguishing these - which leaves you vulnerable to writing a procedure side_effect()
and another developer deciding to abuse the implicit return value of your procedure (basically treating it as an impure function).
To resolve this, take a leaf out of Scala and Haskell's book and have your procedures explicitly return nil
(aka Unit
or ()
in other languages).
If you follow this, then using explicit return
syntax or not just becomes a matter of personal style.
To further distinguish between functions and procedures:
do/end
()
, whereas when you invoke functions, don'tNote that Jörg W Mittag actually advocated the other way around - avoiding ()
s for procedures - but that's not advisable because you want side effecting method invocations to be clearly distinguishable from variables, particularly when arity is 0. See the Scala style guide on method invocation for details.
Like Ben said. The fact that 'the return value of a ruby method is the return value of the last statement in the function body' causes the return keyword to be rarely used in most ruby methods.
def some_func_which_returns_a_list( x, y, z)
return nil if failed_some_early_check
# function code
@list # returns the list
end