3 ms·
I've been very taken by Ruby and how it uses blocks everywhere! This is an article I wrote just to emphasize that.
by stonecharioteer 1y ago
I've been very taken by Ruby and how it uses blocks everywhere! This is an article I wrote just to emphasize that.
- pjmlp 1y agoHave a look at Smalltalk blocks, or FP languages, to see where Ruby's inspiration comes from.
- frou_dh 1y agoAn interesting thing about Smalltalk and Ruby blocks is that they aren't just anonymous functions/lambdas, right? i.e. if you 'return' / '^' in a block, it's the context around the block that you return from, not just the block itself? That's what struck me about both of them when I was used to thinking in basic Lisp terms.
- pjmlp 1y agoYes, that is why that behaviour is known as closures. Also why in languages like C++, you get to control what is captured from the calling context.
- vidarh 1y agoAs much as I love Ruby (and I love it a lot - I right now have Claude fix eigenclass compilation issues in another window in my long-languishing Ruby compiler hobby project), this is also the root of a lot of confusion. Ruby has two types of closures: lambda's, where return returns to the caller, and proc's/blocks where return acts as a return in the defining scope. Beginners often struggle with the distinction between lambda and block, and often will be wildly confused if they ever see "proc" in the wild. Yet lambda and proc both return a Proc object, and if you take the value of a block, you also get a Proc object... Just with different return semantics... It'd be nice if this was a bit clearer.
- stonecharioteer 1y agoMy next post, which is on loops, is about the common stuff with smalltalk as well!
- vidarh 1y agoRuby is basically what happens when Smalltalk and Perl (compare the command line and the "$"-variables for example) has a baby and it inexplicably looks much better than both parents.
- janfoeh 1y agoI discovered Ruby (through Rails) about twenty years ago on the dot. Coming from Perl and PHP it took me a while, but I remember the moment when I had the same realisation you did. I still love this language to bits, and it was fun to relive that moment vicariously through someone elses eyes. Thanks for writing it up!
- stonecharioteer 1y agoI'm glad folks are having fun reading this. I want to write a few more articles, particularly dissecting the internals of Ruby and how amazing it feels.
- PufPufPuf 1y agoTake a look at Kotlin, it perfected this idea
- pjmlp 1y agoWhat Kotlin offers is already present in Scala or other languages from ML linage.