From: "JustJosh (Joshua Stowers) via ruby-core" <ruby-core@...>
Date: 2025-02-27T00:47:52+00:00
Subject: [ruby-core:121186] [Ruby master Feature#21160] Local return from proc

Issue #21160 has been updated by JustJosh (Joshua Stowers).


ufuk (Ufuk Kayserilioglu) wrote in #note-1:
> `next` isn't necessarily the correct thing to use here, `break` is:

I've tried `break` without success - I get `LocalJumpError: break from proc-closure`

Here is a more complete example of what I am doing:
```ruby
class Example
  def self.fulfills_promise(promise_name, &block)
    @@promise_callbacks ||= {}
    @@promise_callbacks[promise_name] = block
  end

  def self.fulfill_promise(promise_name, data)
    puts "Fulfilling promise: #{promise_name}"

    callback = @@promise_callbacks[promise_name]

    if callback.call(data)
      puts 'Complete!'
    else
      puts 'Failed!'
    end
  end

  fulfills_promise :generate_large_image do |image_data|
    puts 'Will finalize large image...'
    break false # Indicate something went wrong
    puts 'Finalized!'
    true
  end
end


Example.fulfill_promise(:generate_large_image, 'image data')
```

----------------------------------------
Feature #21160: Local return from proc
https://bugs.ruby-lang.org/issues/21160#change-112121

* Author: JustJosh (Joshua Stowers)
* Status: Open
----------------------------------------
When writing DSL-style helper methods, I often store block arguments as procs to use as callbacks.
Using `return` in a proc will return from the context it was created in, which is unsuitable in the following example.
Since procs cannot be converted to lambdas, I end up using `next` to return a value from them early.

Example:
``` ruby
fulfills_promise :generate_large_image do |image_data|
  next false if image_data.nil?

  puts 'Saving image..'
  # etc.
end
```

This works but confuses most readers.

I propose introducing an alias for it that is more appropriate for this use case.
Perhaps `pass` or `continue`?

It's worth noting that `return` would work with `fulfills_promise :foo, -> (bar) do`, though it detracts a bit from a DSL's expressiveness.




-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/