Remix.run Logo
inopinatus 20 hours ago

For this audience it may be worth noting that Ruby’s blocks are closures and are passed to methods either anonymously/implicitly or as a named parameter, may be subsequently passed around to any collaborator object, or otherwise deferred/ignored, have the same range of argument arity as methods and lambdas, can even be formed from (and treated similarly to) lambdas, and are thereby fundamental to Ruby’s claim to being a multiparadigm language even as they also betray the Smalltalk roots.

In addition they have nonlocal return semantics, somewhat like a simple continuation, making them ideal for inline iteration and folding, which is how most new Rubyists first encounter them, but also occasionally a source of surprise and confusion, most notably if one mistakenly conflates return with result. Ruby does separately have callcc for more precise control over stack unwinding, although it’s a little known feature.

rubyfan 18 hours ago | parent | next [-]

These were some of the best long running sentences I’ve read in a while. A true rubyist!

rubybaby99 13 hours ago | parent [-]

> A true rubyist!

When it’s all a single paragraph, you’ll know they’ve achieved Rubynirvana.

dragonwriter 8 hours ago | parent | prev [-]

> can even be formed from (and treated similarly to) lambdas

They are also used to create lambdas (even the shorthand stabby-lambda syntax desugars to a call to Kernel#lambda with a block.)

> Ruby does separately have callcc for more precise control over stack unwinding, although it’s a little known feature.

callcc is included in CRuby but has been sidelined from Ruby as a language separate from CRuby as an implementation for a while, with Fibers understood to cover the most important use cases for callcc.