Last active
July 30, 2020 13:27
-
-
Save pietromoro/cb830491a2a32ea9313bb3953129796e to your computer and use it in GitHub Desktop.
Quick ruby enum
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
| module Kernel | |
| def enum(values) | |
| Module.new do |mod| | |
| values.each_with_index{ |v,i| mod.const_set(v.to_s.capitalize, 2**i) } | |
| def mod.inspect | |
| "#{self.name} {#{self.constants.join(', ')}}" | |
| end | |
| end | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage