diff --git a/lib/literal/enum.rb b/lib/literal/enum.rb index a1ef573..ffa436e 100644 --- a/lib/literal/enum.rb +++ b/lib/literal/enum.rb @@ -132,6 +132,10 @@ def fetch(...) def to_proc method(:cast).to_proc end + + def to_h(*args) + @values.dup + end end def initialize(name, value, &block) diff --git a/test/enum.test.rb b/test/enum.test.rb index 57c8a58..c51d0a8 100644 --- a/test/enum.test.rb +++ b/test/enum.test.rb @@ -41,6 +41,8 @@ def toggle = On expect(Color[1]) == Color::Red expect(Color.cast(1)) == Color::Red expect(Color.to_set) == Set[Color::Red, Color::Green, Color::Blue] + expect(Color.to_h) == {1 => Color::Red, 2 => Color::Green, 3 => Color::Blue} + expect(Color.to_a) == [Color::Red, Color::Green, Color::Blue] expect(Color.values) == [1, 2, 3] expect([3, 2, 1].map(&Color)) == [Color::Blue, Color::Green, Color::Red]