Project

General

Profile

Actions

Bug #17595

closed

[Pattern Matching] deconstruct_keys with zero patterns supplies nil for keys argument

Added by baweaver (Brandon Weaver) about 3 years ago. Updated about 3 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:102315]

Description

Example:

Test = Struct.new(:a, :b) do
  def deconstruct_keys(ks)
    p ks: ks
    to_h.slice(*ks)
  end
end
# => Test

Test.new(1,2) in {}
# {:ks=>nil}
# => true

As users may call Array methods against the keys argument I consider this a potential bug that it returns nil for no supplied arguments.

Proposed Patch: Pass an empty array instead to maintain interface consistency.

Updated by baweaver (Brandon Weaver) about 3 years ago

An alternative patch would to be raising an exception as an empty hash for a match feels like an invalid case, as mentioned by k_tsj.

Updated by ktsj (Kazuki Tsujimoto) about 3 years ago

  • Status changed from Open to Rejected

It is intentional behavior, so I close this ticket.

  • When **rest is used in the pattern, nil is passed as a keys value.
  • {} is equivalent to {**rest} if rest.empty?
Actions

Also available in: Atom PDF

Like0
Like0Like0