From: "kddeisz (Kevin Newton)" Date: 2022-07-03T18:04:57+00:00 Subject: [ruby-core:109128] [Ruby master Feature#18773] deconstruct to receive a range Issue #18773 has been updated by kddeisz (Kevin Newton). @mame and @ktsj - I definitely understand your concerns! I'm sorry I didn't make it clear in the initial report, I meant to open this ticket as the beginning of a discussion, not as the definitive solution. I would be very open to discussing other options. Mostly I'm just trying to solve the issue where loading an array with deconstruct is expensive (like deconstruct_keys with the keys argument). I can think of a couple of ways to do this, including the approach in this commit: * Deprecate deconstruct without an argument, eventually pass it a range that functions as @mame described * Deprecate deconstruct without an argument, eventually pass it a size for before, a boolean for *, a size for after * Create a new method deconstruct_indices that receives those arguments, call it if it is defined, otherwise call deconstruct if it is defined * Do nothing and not worry about it ���� I think any of these would be okay. I agree with @mame that creating a method object is probably a bad idea. ---------------------------------------- Feature #18773: deconstruct to receive a range https://bugs.ruby-lang.org/issues/18773#change-98269 * Author: kddeisz (Kevin Newton) * Status: Assigned * Priority: Normal * Assignee: ktsj (Kazuki Tsujimoto) ---------------------------------------- Currently when you're pattern matching against a hash pattern, `deconstruct_keys` receives the keys that are being matched. This is really useful for computing expensive hashes. However, when you're pattern matching against an array pattern, you don't receive any information. So if the array is expensive to compute (for instance loading an array of database records), you have no way to bail out. It would be useful to receive a range signifying how many records the pattern is specifying. It would be used like the following: ```ruby class ActiveRecord::Relation def deconstruct(range) (loaded? || range.cover?(count)) ? records : nil end end ``` It needs to be a range and not just a number to handle cases where `*` is used. You would use it like: ```ruby case Person.all in [] "No records" in [person] "Only #{person.name}" else "Multiple people" end ``` In this way, you wouldn't have to load the whole thing into memory to check if it pattern matched. The patch is here: https://github.com/ruby/ruby/pull/5905. -- https://bugs.ruby-lang.org/ Unsubscribe: