9 ms·
> Blocks are just anonymous functions. That's it. Blocks in ruby have non-local return, unlike anonymous functions. compare `[1,2,3].map {|e| return true if e
by topher6345 6y ago
> Blocks are just anonymous functions. That's it.
Blocks in ruby have non-local return, unlike anonymous functions.
compare `[1,2,3].map {|e| return true if e > 1 }` in ruby to `[1,2,3].map(e => {if(e > 1) { return true}})` in javascript. The ruby version will return a single `true` value, while the javascript version will return an array.
This post walks through a example that resembles my experience writing bugs when I knew way more ruby than javascript: https://github.com/raganwald-deprecated/homoiconic/blob/master/2010/04/blocks.md https://github.com/raganwald-deprecated/homoiconic/blob/mast...
- nlh 6y agoInteresting! I recall reading about this before but I’d forgotten it. And I suppose one of the things that trips people up a lot about ruby is that the implicit return gives a different result — eg your example but without the “return true if” part DOES return an array, like JavaScript.