Project

General

Profile

Actions

Bug #16660

closed

Struct#deconstruct_keys inconsistent behavior

Added by palkan (Vladimir Dementyev) over 4 years ago. Updated over 4 years ago.

Status:
Rejected
Target version:
-
ruby -v:
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]
[ruby-core:97297]

Description

Here is an example of a kind of surprising (at least to me) Struct#deconstruct_keys behaviour:

klass = Struct.new(:a, :b)
s = klass.new(1, 2)
  1. When some keys are not recognized and the total number of the keys is not greater than the size of the struct:
s.deconstruct_keys([:a, :c])
#=> {a: 1}
  1. When some keys are not recognized but the total number of the keys is greater than the size of the struct:
s.deconstruct_keys([:a, :b, :c])
#=> {}

It's not clear why the first one filters unknown keys and the latter one returns an empty Hash instead of the {a: 1}.

This behaviour was introduced in https://github.com/ruby/ruby/commit/2439948bcc0ec9daf91cf79301195e59bad49aff. Prior to that change an empty hash was returned in both cases.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0