https://github.com/alexherbo2/enum.rb
Enum for Ruby
https://github.com/alexherbo2/enum.rb
enum ruby
Last synced: over 1 year ago
JSON representation
Enum for Ruby
- Host: GitHub
- URL: https://github.com/alexherbo2/enum.rb
- Owner: alexherbo2
- Created: 2021-09-22T21:48:33.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-09-26T08:54:11.000Z (almost 5 years ago)
- Last Synced: 2025-02-01T22:28:46.689Z (over 1 year ago)
- Topics: enum, ruby
- Language: Ruby
- Homepage: https://www.ruby-lang.org
- Size: 1000 Bytes
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Enum for Ruby
A [Ruby] implementation of [enumerated type] for educational purposes.
[Ruby]: https://www.ruby-lang.org
[Enumerated type]: https://en.wikipedia.org/wiki/Enumerated_type
Heavily based on [Crystal].
[Crystal]: https://crystal-lang.org
## Overview
An enum is a set of integer values, where each value has an associated name.
For example:
``` ruby
require 'enum'
class Color < Enum
member :Red # 0
member :Green # 1
member :Blue # 2
end
```
Values start with the value `0` and are incremented by one, but can be overwritten.
To get the underlying value you invoke `value` on it:
``` ruby
Color::Green.value # ⇒ 1
```
### Enums from integers
An enum can be created from an integer:
``` ruby
Color.new(1).to_s # ⇒ "Green"
```
Values that don’t correspond to enum’s constants are allowed:
the value will still be of type `Color`, but when printed you will get the underlying value:
``` ruby
Color.new(10).to_s # ⇒ "10"
```
This method is mainly intended to convert integers from C to enums in [Ruby].
### Question methods
``` ruby
Color::Red.red? # ⇒ true
Color::Blue.red? # ⇒ false
```
## Usage
``` ruby
def paint(color)
case color
when Color::Red
# ...
else
# Unusual, but still can happen.
raise "Unknown color: #{color}"
end
end
```
## Reference
- [Crystal reference]
- [Crystal API]
[Crystal reference]: https://crystal-lang.org/reference/syntax_and_semantics/enum.html
[Crystal API]: https://crystal-lang.org/api/master/Enum.html