Skip to content

Commit 239c931

Browse files
authored
Merge pull request #63 from dblock/fix-values-override-issue-62
Fix values, value? and key returning a superclass' overridden value
2 parents 9b8676c + c3268a5 commit 239c931

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 1.2.1 (Next)
22

3+
* [#62](https://github.com/dblock/ruby-enum/issues/62): Fixed `values`, `value?` and `key` to not return a superclass' overridden value when a subclass redefines a key, matching the behavior of `to_h` and other methods - [@dblock](https://github.com/dblock).
34
* Your contribution here.
45

56
### 1.2.0 (2026/8/14)

‎lib/ruby-enum/enum.rb‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,9 @@ def keys
124124
_enum_hash.values.map(&:key)
125125
end
126126

127-
# Returns all enum values.
127+
# Returns all enum values, including those defined in a superclass.
128128
def values
129-
result = _own_enum_hash.values.map(&:value)
130-
131-
if superclass < Ruby::Enum
132-
superclass.values + result
133-
else
134-
result
135-
end
129+
_enum_hash.values.map(&:value)
136130
end
137131

138132
# Iterate over all enumerated values, including those defined in a superclass.
@@ -186,12 +180,16 @@ def _enum_hash
186180
# Returns the enums-by-value hash for this class merged with all of its
187181
# superclasses, with values defined in this class taking precedence over
188182
# those inherited from a superclass.
183+
#
184+
# Derived from _enum_hash (keyed by key) rather than merged directly by
185+
# value, so that a superclass' stale value for a key overridden in this
186+
# class doesn't linger, e.g. when a subclass redefines a key with a new
187+
# value, the superclass' old value should no longer be found via value?
188+
# or key.
189189
def _enums_by_value
190-
@_enums_by_value ||= if superclass < Ruby::Enum
191-
superclass.send(:_enums_by_value).merge(_own_enums_by_value)
192-
else
193-
_own_enums_by_value
194-
end
190+
@_enums_by_value ||= _enum_hash.each_with_object({}) do |(_key, enum), hash|
191+
hash[enum.value] = enum
192+
end
195193
end
196194

197195
def upper?(s)

‎spec/ruby-enum/enum_spec.rb‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,15 @@ class EmptyEnums
432432
expect(SubclassRedefiningParentKey.to_h).to eq(RED: 'crimson', GREEN: 'green')
433433
end
434434

435-
it 'includes both the parent and its own redefined value in values' do
436-
expect(SubclassRedefiningParentKey.values).to eq(%w[red green crimson])
435+
it "does not include the parent's overridden value, only its own redefined value, in values" do
436+
expect(SubclassRedefiningParentKey.values).to eq(%w[crimson green])
437+
end
438+
439+
it "does not recognize the parent's overridden value via value? or key" do
440+
expect(SubclassRedefiningParentKey.value?('red')).to be false
441+
expect(SubclassRedefiningParentKey.key('red')).to be_nil
442+
expect(SubclassRedefiningParentKey.value?('crimson')).to be true
443+
expect(SubclassRedefiningParentKey.key('crimson')).to eq :RED
437444
end
438445

439446
it "the parent class' own value is unaffected" do

0 commit comments

Comments
 (0)