[ruby-core:96590] [Ruby master Misc#16464] Which core objects should support deconstruct/deconstruct_keys?
From:
fg@...
Date:
2019-12-30 07:54:27 UTC
List:
ruby-core #96590
Issue #16464 has been updated by decuplet (Nikita Shilnikov).
These probably should be filed as separate feature requests. I'm already using PM in production and the idea about deconstructing `Time` instances also came into my mind. `Object#deconstruct` is a lot more subtle, I wouldn't say it's _that_ obvious, I'd rather have it for some built-in classes such as `Time` or `Integer`.
----------------------------------------
Misc #16464: Which core objects should support deconstruct/deconstruct_keys?
https://bugs.ruby-lang.org/issues/16464#change-83557
* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
Now, when pattern matching is out, I believe it is worth discussing which core and standard library objects should be matchable "out of the box".
My proposals, as of now, are:
**1. `Object#deconstruct`, returning `[self]`.**
Justification:
```ruby
# this works:
1 in Integer
# this works:
1 in ..0
# this does NOT:
1 in Integer(..0)
# NoMatchingPatternError (1)
```
I believe the latter example looks pretty logical (and can be used in some flexible methods like "if it is a positive integer, it is index in the array, if it is negative integer, it is backward index, and if it is float, it should be calculated as a mean of nearby elements")
**2. `Time#deconstruct_keys`**
Justification is obvious:
```ruby
case created_at
when year: 2019, month: 11..12 => m, day:
p "Created at #{day}.#{m} this year"
else
# ...
end
```
(Probably the same for `Date` and `DateTime`)
**3. `Set#deconstruct`**
Seems "logical" as set is a sequence, but I can't think of a good realistic example :)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>