The return keyword is optional in ruby so for functions with only one exit point, "return result" can be safely replaced with simply "result".
Is there any Ruby-specific guidelines to when to do 开发者_运维知识库this?
I tend to avoid the return keyword as much as possible due to their unruly behavior in procs.
"return" in ruby is only used if you are trying to return more than one value. e.g.
return val1, val2
or if it makes sense to return earlier from a function e.g.
#check if needed param is set
return if !param
#some operations which need param
which is easier than messing your code up with cascaded if-statements.
Conclusion: Use return every time it simplifies your code or it makes it easier to understand.
精彩评论