4 ms·
Without some difference between functions and blocks, patterns like foo.each do |x| return if x == 3 end Don't "return" out of the enclosing function.
by evmar 12y ago
Without some difference between functions and blocks, patterns like
foo.each do |x|
return if x == 3
end
Don't "return" out of the enclosing function.
- baddox 12y agoThat seems okay to me. JavaScript works well enough with that behavior.
- masklinn 12y agoJavascript iteration primitive isn't block-based.
- picks_at_nits 12y agoRuby iteration primitive isn’t block based, either.
- masklinn 12y agoOf course it is. `for a in b` desugars to `b#each`, not the other way around.
- picks_at_nits 12y agoHow do “while” and “until” desugar? Kernel#loop?
- chrisseaton 12y agoThose are two rare exceptions and you're right they don't desugar.
- picks_at_nits 12y agoThose 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.
- delsalk 12y agoWorks well meaning don't use functional calls at all if you want to escape from them.
- baddox 12y agoI'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).
- delsalk 12y agoIt 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.
- baddox 12y agoWhat 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.
- i_s 12y agoYehuda Katz argues that it does not: http://yehudakatz.com/2012/01/10/javascript-needs-blocks/ http://yehudakatz.com/2012/01/10/javascript-needs-blocks/