Re:My Weird Ruby
by evmar on Friday March 06, 2015 @11:31PM
(#9160991)
Without some difference between functions and blocks, patterns like
Don't "return" out of the enclosing function.
[ Parent
| on HN ]
That seems okay to me. JavaScript works well enough with that behavior.
Yehuda Katz argues that it does not: http://yehudakatz.com/2012/01/10/javascript-needs-blocks/
Works well meaning don't use functional calls at all if you want to escape from them.
I've just never encountered that use case in Ruby code, and to me it feels like really bad style. You can use .find to find the first element that matches a block, then return that item (if one exists).
It doesn't look like fantastic Ruby code, you're right. I just wouldn't agree that JS "works well" when it comes to their somewhat afterthought functional iterators.
What doesn't work well? The only thing I can think of, which comes up on occasion, is that you can't manually stop iteration in forEach without ugly tricks like throwing and catching an exception. I actually wanted that behavior for the first time recently, so I just used JQuery's each which lets you stop iteration by returning false. I'm assuming other JS utility libraries like underscore provide something similar.
Javascript iteration primitive isn't block-based.
Re:My Weird Ruby
by picks_at_nits on Saturday March 07, 2015 @06:55AM
(#9161638)
Ruby iteration primitive isn’t block based, either.
Of course it is. `for a in b` desugars to `b#each`, not the other way around.
Re:My Weird Ruby
by picks_at_nits on Saturday March 07, 2015 @07:33AM
(#9161694)
How do “while” and “until” desugar? Kernel#loop?
Re:My Weird Ruby
by chrisseaton on Saturday March 07, 2015 @10:39AM
(#9162008)
Those are two rare exceptions and you're right they don't desugar.
Re:My Weird Ruby
by picks_at_nits on Saturday March 07, 2015 @11:45AM
(#9162240)
Those are not “exceptions” to Ruby’s primitives, those are Ruby’s primitives. Things that desugar to methods aren’t primitives.
The thing is, they’re rare in practice for cultural reasons.