Skip to content

Inherit keys, key?, value?, key, value, to_h, parse and each from superclass - #59

Merged
dblock merged 1 commit into
masterfrom
subclass-inherited-values
Aug 15, 2026
Merged

Inherit keys, key?, value?, key, value, to_h, parse and each from superclass#59
dblock merged 1 commit into
masterfrom
subclass-inherited-values

Conversation

@dblock

@dblock dblock commented Aug 15, 2026

Copy link
Copy Markdown
Owner

Summary

Follow-up to #49 / #58.

values already merges enums from a class' entire superclass chain, but keys, key?, value?, key, value, to_h, parse and each only looked at a class' own enums, silently ignoring anything defined in a superclass:

class Colors
  include Ruby::Enum
  define :RED, 'red'
  define :GREEN, 'green'
end

class FirstSubclass < Colors
  define :ORANGE, 'orange'
end

FirstSubclass.values  # => ["red", "green", "orange"]  (correct, already worked)
FirstSubclass.keys    # => [:ORANGE]                   (missing :RED, :GREEN!)
FirstSubclass.key?(:RED) # => false                    (wrong)

Fix

Introduces _enum_hash/_enums_by_value private methods that merge a class' own enums with those of its superclass chain (a subclass' own definitions take precedence over an identically-named key/value defined in a superclass). All read methods (keys, key?, value?, key, value, to_h, parse, each, each_key, each_value) now use these merged views, consistent with values.

define's duplicate-key/duplicate-value validation still only looks at the class' own enums (via new _own_enum_hash/_own_enums_by_value), preserving the existing (and tested) ability for a subclass to redefine a key or value already used by a superclass.

Testing

@github-actions

github-actions Bot commented Aug 15, 2026

Copy link
Copy Markdown

Danger Report

No issues found.

View run

…erclass

Follow-up to #49/#58. values already merged enums from a superclass
chain, but keys, key?, value?, key, value, to_h, parse and each only
looked at a class' own enums, silently ignoring any enums defined in
a superclass.

Introduces _enum_hash/_enums_by_value that merge a class' own enums
with those of its superclass chain (subclass definitions take
precedence over the same key/value defined in a superclass), and uses
them for all read methods. define/duplicate validation continues to
use the class' own enums only (_own_enum_hash/_own_enums_by_value), so
a subclass may still redefine a key or value already used by a
superclass.

Documents the change in README.md and UPGRADING.md, and adds tests
covering a subclass redefining a parent class' key.

Co-authored-by: Copilot <[email protected]>
@dblock
dblock force-pushed the subclass-inherited-values branch from 2ce77b1 to e11b310 Compare August 15, 2026 01:26
@dblock
dblock merged commit 28d00d7 into master Aug 15, 2026
22 checks passed
@dblock
dblock deleted the subclass-inherited-values branch August 15, 2026 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant