Project

General

Profile

Actions

Feature #21160

open

Local return from proc

Added by JustJosh (Joshua Stowers) 8 days ago. Updated 7 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:121184]

Description

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:

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0