Skip to content

Commit

Permalink
Implement Enum.to_h (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegeek authored Jun 28, 2024
1 parent 3927f33 commit a4a7cf6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/literal/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions test/enum.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit a4a7cf6

Please sign in to comment.