Project

General

Profile

Actions

Bug #17595

closed

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

Added by baweaver (Brandon Weaver) 10 months ago. Updated 10 months ago.

Status:
Rejected
Priority:
Normal
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) 10 months 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) 10 months 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