Project

General

Profile

Actions

Misc #18609

closed

keyword decomposition in enumerable (question/guidance)

Added by Ethan (Ethan -) about 2 years ago. Updated about 2 years ago.

Status:
Closed
Assignee:
-
[ruby-core:107769]

Description

There is a pattern that I have used somewhat often in ruby 2, decomposing hash keys as keyword arguments to blocks for Enumerable, which no longer works in ruby 3. I'm wondering if there is a better way that I am missing (I couldn't find any discussion of this particular thing searching this tracker).

drafts = [
  {name: 'draft4', mod: :Draft04, image: 'draft4.png'},
  {name: 'draft6', mod: :Draft06, image: 'draft6.jpg'},
]

# ruby 2
drafts.each do |name: , mod: , image: |
  ...

# ruby 3
drafts.each do |draft|
  name = draft[:name]
  mod = draft[:mod]
  image = draft[:image]
  ...

the latter is much more cumbersome, but seems necessary with the switch in keyword argument handling in ruby 3.

I can refactor to name, mod, image = draft[:name], draft[:mod], draft[:image]. that is a little better but still more verbose and repetitive than it used to be, and with more keys the line gets very long.

I am expecting this pattern is just a casualty of the keyword split that I will have to rewrite and this issue can be closed, but hoping there may be some better option I have missed.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0