Hacker Newsnew | past | comments | ask | show | jobs | submit | drnewman's commentslogin

I'd think the preferred way would be to implement them as a separate class. That'd be more in line with the design of other core classes (like TrueClass & FalseClass). It'd also have created the opportunity to create a "Callable" protocol early in the design which would have been great, and would harmonize with the design of Ruby's many other protocols Enumerable, Comparable, Range-compatible values, etc.


Yeah, I think I've heard that too.


That's insightful


It seems you're pretty upset about your experience with Ruby. I'm sorry that's been the case for you.

However, in Ruby blocks aren't just about flexibility, more importantly they're about generality. They're not there to resolve an edge case at all (Ruby also has keywords for loops). They're a generalization of control flow that is just slightly less general than continuations. In practical use they provide an alterative to Lisp macros for many use cases.

These were some of the problems Matz was trying to sort out with his design of Ruby--creating a language that was fun and easy to use for day-to-day programming, with as much of the meta-programming power of Lisp and Smalltalk as possible. Blocks are one of his true innovatations that came from trying to balance that tension.


> Blocks are one of his true innovatations …

How do they differ from Smallalk blocks? (I don't know.)

https://drcuis.github.io/TheCuisBook/Block-syntax.html


Blocks in Smalltalk (to my understanding) are closures. Blocks in Ruby are closures that also bring the call stack they were created in with them.

One way to think of about it is this: anonymous functions as originally implemented in early Lisps are code as an object, closures are code with its lexical environment as an object. You can think of a Ruby block as code with its lexical environment and its call stack as an object.

So they don't just handle return differently than closures, they have access to the call stack of the site where they're created like a continuation. This is why they handle return differently, but this is just one of the things that falls out from that. It also comes with other control flow features like "redo", "retry", "next", "rescue", "finally", and others. These are all call stack control (control flow) conveniences, just like return is. All of them can be thought of as being abstractions built on top of continuations (just ask a Scheme hacker).

Originally Ruby was basically a Lisp without macros, but with continuations, a Smalltalk like object system and a lot of syntactic affordances inspired by Perl, and other languages. Blocks are one of the conveniences built on top of the Lispy semantics.

Note that I'm explaining how blocks work as an abstraction (vidarh below explains how they work as a concretion, as implemented in MRI).


> … other control flow features like "redo", "retry", "next", "rescue", "finally", and others.

At-a-glance afaict Smalltalk provides those features too, so I would guess Smalltalk blocks may have access to the call stack too?

https://drcuis.github.io/TheCuisBook/The-Debugger.html


Yes Smalltalk has continuations. So it can do all of those things as well. But I don't think they're explicitly tied to blocks like they are in Ruby. This really isn't a problem for Smalltalk since it's not as syntax oriented as Ruby.

The invovation is to have those features tied to convenient syntax.


So is the innovation to make something that was available in Lisps / Smalltalks, available within the different constraints of Ruby.

(I should check how Smalltalk blocks behave.)


I would say more broadly the innovation was two fold: 1) to make these features available in a syntactic form that would seem more familiar to programmers and 2) the powerful insight that when combined with Smalltalk style meta programming you can have a language that on the surface seems very conventional but underneath is just as powerful as Smalltalk or Lisp.

Although I would say he didn’t get 100% there although that this point Ruby isn’t too far from that.

These are ideas that I think are worth trying to take even further. In fact, I’ve been experimenting with that.


fwiw "Efficient Implementation of Smalltalk Block Returns"

https://wirfs-brock.com/allen/things/smalltalk-things/effici...


I’ll check it out. Thank you.


I don't believe this is correct. Blocks are closures.

Continuations were introduced in Ruby 1.8, via a callcc method in a Kernel module or something like that.

Since 1.9 they are in some sort of deprecated status.

I don't think that even the yield() stuff is implemented using continuations.


I agree blocks are closures. But they’re call stack aware closures. Which gives them _some_ of the power of continuations.

I’m also sure you’re right that they’re not implemented using continuations. However, my understanding is that Ruby was originally conceived as a language with continuations. I’ll see if I can find a reference for that. But from what I recall reading in a blog post from someone who was at a programming language conference in 1997 when Matz introduced the language that’s how he described it.


> they’re call stack aware closures

What does that even mean?

If you create a block deeply within a cascade of nested function calls, nineteen activation levels deep, and return that block out of all those nestings, is it still aware of the nineteen levels that have terminated, and to what purpose/benefit?

What example Ruby code would break without continuing access to the dynamic scope that has terminated, rather than just the lexical scope?


> If you create a block deeply within a cascade of nested function calls, nineteen activation levels deep, and return that block out of all those nestings, is it still aware of the nineteen levels that have terminated, and to what purpose/benefit?

A block cannot be returned, because it is not an object; it has to be used to create a Proc object with either proc or lambda semantics to be returned.

With lambda semantics, its just a closure, doesn't care about the dynamic scope, and returns immediately to whatever calls it with return/next, or returns from the calling method with break.

With proc semantics, it does retain the connection, and return or break will result in an error when those scopes have terminated, but next will still return to the caller. (You don't generally want to return a proc for that reason, the use for procs is passing down a call chain, not returning them up.)


Nice explanation


OK, so we have gone from "blocks are continuations!" to "blocks (by themselves) are a kind of downward-funarg-only thunk!" (that can be used to specify the code part of a lambda or Proc, which are not continuations either).


No, return exiting the enclosing function scope from a block is the special case that's there to resolve an edge case.

Blocks should be nothing special. They're anonymous functions that capture the environment mutably. The only new part is all the special bits added to handle the weird edge cases that they're trying to pretend don't exist.


I disagree that it's just there to handle edge cases. It's a useful generalization.

I think the "Building an intuition" section of my blog post[1] makes a good case for that.

  When dealing with loops, you have 3 nested constructs interacting: a wrapping function, a loop statement and the loop's body; and you have 3 keywords to choose where the flow of the code goes.

    return returns from the wrapping function
    break leaves the loop statement
    next / continue leaves loop's body

  When dealing with blocks or anonymous functions, it's instead 3 nested "functions" that are interacting: a wrapping function, a called function and an anonymous functions (or block).

    Ruby's blocks, let you use the same 3 keywords to choose where the flow of the code goes.

    return returns from the wrapping function (ex: my_func)
    break returns from the called function (ex: each, map)
    next returns from the block

  Quite consistent. But since we are talking about functions instead of statements (loop), return values are also involved. Allowing both break and next to provide a return value fits well in that model and is quite useful. The 3 keywords are basically return, but they have different targets.
[1] https://maxlap.dev/blog/2022/02/10/what-makes-ruby-blocks-gr...


> No, return exiting the enclosing function scope from a block is the special case that's there to resolve an edge case.

What is the edge case? It seems to be there so that Ruby enumeration methods can provide the behavior expected of looping statements (which is kind of necessary if you want the looping statements to just be semantic sugar for enumeration methods so that they can work correctly with any enumerable.)

> Blocks should be nothing special.

"Special" compared to what?


Laziness is great for collections, and modeling the infinite but as a general model for programming it's a little loopy for my taste too ;-)


SEEKING WORK | Maine | Remote

Software Engineer, Product Designer & Analyst able to help clients complete stalled projects, design & deliver user-friendly applications, and work through tough problems.

Looking for short term engagements

Résumé/CV: https://delonnewman.name/resume.pdf

Email: contact@delonnewman.name

Portfolio: https://delonnewman.name

Github: https://github.com/delonnewman

Highlights:

- Over 23 years of experience

- Over 9 years of experience in Healthcare

- Able to communicate effectively with technical and non-technical personnel

- Experienced tech lead, mentor & teacher

- Polyglot developer, specializing in Ruby & JavaScript

- Enjoy working with arcane data formats (e.g. EDI)

- System integration & automation


  Location: Maine
  Remote: Yes
  Willing to relocate: No

  Technologies:
    Primary Languages: Ruby, JavaScript (TypeScript)
    Databases: SQL (PostgreSQL, MySQL, MS SQL, Oracle), Datomic, Redis, MongoDB
    Infrastructure: AWS, Docker, Linux SysAdmin
    OS: Linux, macOS, iOS
    Back-end: Rails, Sinatra, Node.js, Express
    Front-end: Vanilla, HTMX, React, Svelte, ClojureScript
    Other Languages: Clojure, Java, C, PHP, Perl, Bash, C#, Python, Swift, VBA

  Résumé/CV: https://delonnewman.name/resume.pdf
  Email: contact@delonnewman.name
  Portfolio: https://delonnewman.name
  Github: https://github.com/delonnewman
  LinkedIn: https://www.linkedin.com/in/delonnewman

  Software Engineer, Product Designer & Analyst able to help clients complete stalled projects,
  design & deliver user-friendly applications, and work through tough problems.

  - Over 23 years of experience
  - Over 9 years of experience in Healthcare
  - Experienced tech lead, mentor & teacher
  - Polyglot developer
  - Enjoys working with arcane data formats (e.g. EDI)
  - System architecture & design
  - Looking for part-time or short term engagements


SEEKING WORK | Maine | Remote

Software Engineer, Product Designer & Analyst able to help clients complete stalled projects, design & deliver user-friendly applications, and work through tough problems.

Looking for part-time & short term engagements

Résumé/CV: https://delonnewman.name/history

Email: contact@delonnewman.name

Portfolio: https://delonnewman.name

Github: https://github.com/delonnewman

Highlights:

- Over 23 years of experience

- Over 9 years of experience in Healthcare

- Able to communicate effectively with technical and non-technical personnel

- Experienced tech lead, mentor & teacher

- Polyglot developer, specializing in Ruby & JavaScript

- Enjoy working with arcane data formats (e.g. EDI)

- System integration & automation


  Location: Maine
  Remote: Yes
  Willing to relocate: No

  Technologies:
    Primary Languages: Ruby, JavaScript (TypeScript)
    Databases: SQL (PostgreSQL, MySQL, MS SQL, Oracle), Datomic, Redis, MongoDB
    Infrastructure: AWS, Docker, Linux SysAdmin
    OS: Linux, macOS, iOS
    Back-end: Rails, Sinatra, Node.js, Express
    Front-end: Vanilla, HTMX, React, Svelte, ClojureScript
    Other Languages: Clojure, Java, C, PHP, Perl, Bash, C#, Python, Swift, VBA

  Résumé/CV: https://delonnewman.name/history
  Email: contact@delonnewman.name
  Portfolio: https://delonnewman.name
  Github: https://github.com/delonnewman
  LinkedIn: https://www.linkedin.com/in/delonnewman

  Software Engineer, Product Designer & Analyst able to help clients complete stalled projects,
  design & deliver user-friendly applications, and work through tough problems.

  - Over 23 years of experience
  - Over 9 years of experience in Healthcare
  - Experienced tech lead, mentor & teacher
  - Polyglot developer
  - Enjoys working with arcane data formats (e.g. EDI)
  - System architecture & design
  - Looking for part-time or short term engagements


Thank you for clarifying that.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: