Project

General

Profile

Actions

Feature #19002

open

Explicit splat for enumerator kwarg blocks

Added by inopinatus (Joshua GOODALL) over 2 years ago. Updated over 2 years ago.

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

Description

I'm renovating some 2.x-era code that relied heavily on autosplat for block kwargs. As we all know, this throws an ArgumentError in Ruby 3:

ary = [
  { foo: 42, bar: 101 },
  { foo: 99, bar: 123 }
]

ary.map { |foo:, bar:| foo+bar } #=> ArgumentError

The nicest solution I could find so far is

module Enumerable
  def splat
    each { |arg| yield **arg }
  end
end

and then

ary.map.splat { |foo:, bar:| foo+bar } #=> [143, 222]

and I present this as a feature suggestion.

Actions #1

Updated by inopinatus (Joshua GOODALL) over 2 years ago

  • Description updated (diff)
Actions #2

Updated by inopinatus (Joshua GOODALL) over 2 years ago

  • Description updated (diff)

Updated by inopinatus (Joshua GOODALL) over 2 years ago

I also had

module Enumerable
  def **(proc) = each { |arg| proc[**arg] }
end

ary.map ** ->(foo:, bar:) { foo+bar } #=> [143, 222]

which is kinda fun, but not so readable.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0