Remix.run Logo
hshdhdhehd 20 hours ago

Is a block basically a lambda or is there more to it?

masklinn 20 hours ago | parent | next [-]

Assuming that by lambda you mean "an anonymous function"[1], for most intents and purposes they are, except their returns are non-local whereas functions usually have local returns.

However blocks are special forms of the language, unless reified to procs they can only be passed as parameter (not returned), and a method can only take one block. They also have some oddities in how they interact with parameters (unless reified to lambda procs).

[1] because Ruby has something called "lambda procs"

simonask 20 hours ago | parent | prev | next [-]

They are closures. But Ruby can do interesting and slightly reckless things, like transplanting a closure into a different evaluation scope. It’s very powerful, and also very dangerous in the wrong hands.

hshdhdhehd 20 hours ago | parent [-]

Sounds a bit like a lisp macro? Or in JS using eval?

somewhereoutth 20 hours ago | parent | prev [-]

My understanding is that the 'extra thing' is control flow - blocks can force a return in their calling scope. For example a loop that calls a block may be terminated/skipped by a break/continue statement in the block itself. However I'm not a Ruby programmer, so please check my working.