Project

General

Profile

Actions

Misc #16464

closed

Which core objects should support deconstruct/deconstruct_keys?

Added by zverok (Victor Shepelev) almost 5 years ago. Updated almost 2 years ago.

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

Description

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:

# 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:

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 :)

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0