You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 28d00d7
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
### 1.2.0 (Next)
2
2
3
3
*[#49](https://github.com/dblock/ruby-enum/issues/49): Fixed `NoMethodError` on `keys`, `values`, `key?`, `value?`, `key`, `value`, `to_h`, `parse` and `each` when a subclass defines no enums of its own - [@dblock](https://github.com/dblock).
4
+
*[#49](https://github.com/dblock/ruby-enum/issues/49): `keys`, `key?`, `value?`, `key`, `value`, `to_h`, `parse` and `each` now include enums defined in a superclass, matching the existing behavior of `values` - [@dblock](https://github.com/dblock).
Copy file name to clipboardExpand all lines: README.md
+21-1Lines changed: 21 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -232,7 +232,7 @@ The `DuplicateValueError` exception is raised to be consistent with the unique k
232
232
233
233
### Inheritance
234
234
235
-
When inheriting from a `Ruby::Enum` class, all defined enums in the parent class will be accessible in sub classes as well. Sub classes can also provide extra enums, as usual.
235
+
When inheriting from a `Ruby::Enum` class, all defined enums in the parent class will be accessible in subclasses as well. Subclasses can also provide extra enums, as usual.
All other enumerating and hashing methods (`keys`, `key?`, `value?`, `key`, `value`, `to_h`, `parse` and `each`) also consider enums defined anywhere in the class hierarchy.
A subclass may redefine a key or value already used by a parent class without raising `DuplicateKeyError` or `DuplicateValueError`; its own definition takes precedence.
274
+
275
+
```ruby
276
+
classShippedOrderState < OrderState
277
+
define :CREATED, 'RECREATED'# does not raise, overrides the parent class' definition
If you want to make sure that you cover all cases in a case stament, you can use the exhaustive case matcher: `Ruby::Enum::Case`. It will raise an error if a case/enum value is not handled, or if a value is specified that's not part of the enum. This is inspired by the [Rust Pattern Syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html). If multiple cases match, all matches are being executed. The return value is the value from the matched case, or an array of return values if multiple cases matched.
This only applies to classes that inherit from another which is a `Ruby::Enum`.
8
+
9
+
Prior to version `1.2.0`, only `values` enumerated enums defined in the entire class hierarchy; `keys`, `key?`, `value?`, `key`, `value`, `to_h`, `parse` and `each` only considered enums defined directly on the class, silently ignoring anything defined in a superclass.
10
+
11
+
As of version `1.2.0`, these methods behave consistently with `values` and also enumerate/consider enums defined anywhere in the class hierarchy, ancestors first. A subclass may still redefine a key or value already used by a superclass; its own definition takes precedence.
12
+
13
+
```ruby
14
+
classPrimaryColors
15
+
includeRuby::Enum
16
+
17
+
define :RED, 'RED'
18
+
define :GREEN, 'GREEN'
19
+
end
20
+
21
+
classRainbowColors < PrimaryColors
22
+
define :ORANGE, 'ORANGE'
23
+
end
24
+
```
25
+
26
+
`gem 'ruby-enum', '< 1.2.0'`
27
+
28
+
```ruby
29
+
RainbowColors.keys # => [:ORANGE]
30
+
RainbowColors.key?(:RED) # => false
31
+
```
32
+
33
+
`gem 'ruby-enum', '>= 1.2.0'`
34
+
35
+
```ruby
36
+
RainbowColors.keys # => [:RED, :GREEN, :ORANGE]
37
+
RainbowColors.key?(:RED) # => true
38
+
```
39
+
40
+
See [#49](https://github.com/dblock/ruby-enum/issues/49) for more information.
0 commit comments