From: "baweaver (Brandon Weaver)" Date: 2022-03-04T08:45:37+00:00 Subject: [ruby-core:107774] [Ruby master Misc#18609] keyword decomposition in enumerable (question/guidance) Issue #18609 has been updated by baweaver (Brandon Weaver). sawa (Tsuyoshi Sawada) wrote in #note-2: > Perhaps, you can also do this: > > ```ruby > drafts.each do |draft| > name, mod, image = draft.values_at(:name, :mod, :image) > ... > ``` > > or if you are sure about the order of the keys, even this (although fragile): > > ```ruby > drafts.each do |draft| > name, mod, image = draft.values > ... > ``` If we want to be _really_ interesting we can take a note from pattern matching on those: ```ruby drafts.each do |name:, mod: { id:, creator: }, image: { url: }| # ... end ``` But that's pretty overkill and Whitequark / parser creators would hate us for it. Anyways, if it's a regression from 3.1.0 to 3.1.1 should probably be patched back, as that's been something I've used myself in the past too. ---------------------------------------- Misc #18609: keyword decomposition in enumerable (question/guidance) https://bugs.ruby-lang.org/issues/18609#change-96695 * Author: Ethan (Ethan -) * Status: Open * Priority: Normal ---------------------------------------- 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). ```ruby 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. -- https://bugs.ruby-lang.org/ Unsubscribe: