Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats up with implicit return values in Ruby?

So I've started looking at ruby, and a lot of things look nice but I'm quite put off by implicit return statements. I understand making everything return self or nil by default but not the last value of a statement.

To me it looks horribly fragile (especially) if you are working with a method that doesn't plan to return something (especially a function which changes state/destructive method !) , other people may end up relying on a return that is not important to the purpose of the method and has a decent chance of changing.

What's the point of implicit return? Is there a way it makes some things way simpler?

Is it considered good practice to always have a return to prevent implicit returns?

Am I worrying to much about this?

P.S. Do people often use implicit return when they mean to return a certain thing from a method, doesn't this make it easier for other people in your group to break each others code? Sure , document everything and give meaningful names you might say, but people don't always do that in real life.

like image 214
Roman A. Taycher Avatar asked Jun 11 '10 07:06

Roman A. Taycher


2 Answers

Make sure not to add puts statements to the end of a method or a block unless you want to return nil.

like image 200
Andrew Grimm Avatar answered Oct 15 '22 16:10

Andrew Grimm


You're worrying too much. In my ~5yrs ruby practice I have'nt had a case when I misrelied on some return value.

And it makes methods to look really self-explaining and improves readability. I think that's very natural.

like image 30
zed_0xff Avatar answered Oct 15 '22 15:10

zed_0xff