Fix NoMethodError on enum methods when subclass defines no enums of its own - #58
Merged
Conversation
Ruby::Enum's class methods (keys, values, key?, value?, key, value, to_h, parse, each) relied directly on @_enum_hash/@_enums_by_value instance variables, which are only set the first time define is called. A subclass that includes Ruby::Enum only through inheritance and never calls define itself never gets these ivars set, so calling any of the above methods raised NoMethodError on nil. Introduces private _enum_hash/_enums_by_value reader methods that default to an empty hash, and uses them everywhere instead of the raw instance variables. Closes #49. Co-authored-by: Copilot <[email protected]>
Danger ReportNo issues found. |
This was referenced Aug 15, 2026
dblock
added a commit
that referenced
this pull request
Aug 15, 2026
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #49.
Ruby::Enum's class methods (keys,values,key?,value?,key,value,to_h,parse,each) read directly from the@_enum_hash/@_enums_by_valueinstance variables. These are only initialized the first timedefineis called on a class (or explicitly in theincludedhook).A subclass that inherits
Ruby::Enumbehavior purely through Ruby class inheritance - without callingdefineitself and without re-includingRuby::Enum- never has these ivars set on its own singleton class, so any of the above methods raisedNoMethodError: undefined method '...' for nil.Fix
Introduces private
_enum_hash/_enums_by_valuereader methods that lazily default to{}, and uses them everywhere instead of the raw instance variables. This preserves all existing behavior (includingvalues' superclass-aware recursion) while making the other methods null-safe for subclasses with no enums of their own.Testing
SubclassWithNoOwnDefinestest class and specs coveringvaluesplus a regression test thatkeys,key?,value?,key,value,to_h,parse, andeachno longer raise.bundle exec rspec spec/- 55 examples, 0 failures, 100% line coverage.bundle exec rubocop- clean.spec_i18nsuite - 3 examples, 0 failures.