开发者

Are return statements bad in Ruby?

开发者 https://www.devze.com 2023-03-20 16:06 出处:网络
The return keyword is optional in ruby so for functions with only one exit point, \"return result\" can be safely replaced with simply \"result\".

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号