A handy way to define enums in Ruby.
Enum-like behavior for Ruby, heavily inspired by this, and improved upon another blog post.
Enums can be defined and accessed either as constants, or class methods, which is a matter of preference.
Define enums, and reference them as constants.
class OrderState
include Ruby::Enum
define :CREATED, 'created'
define :PAID, 'paid'
endOrderState::CREATED # 'created'
OrderState::PAID # 'paid'
OrderState::UNKNOWN # raises Ruby::Enum::Errors::UninitializedConstantError
OrderState.keys # [ :CREATED, :PAID ]
OrderState.values # [ 'created', 'paid' ]
OrderState.to_h # { :CREATED => 'created', :PAID => 'paid' }Define enums, and reference them as class methods.
class OrderState
include Ruby::Enum
define :created, 'created'
define :paid, 'paid'
endOrderState.created # 'created'
OrderState.paid # 'paid'
OrderState.undefined # NoMethodError is raised
OrderState.keys # [ :created, :paid ]
OrderState.values # ['created', 'paid']
OrderState.to_h # { :created => 'created', :paid => 'paid' }The value is optional. If unspecified, the value will default to the key.
class OrderState
include Ruby::Enum
define :UNSPECIFIED
define :unspecified
endOrderState::UNSPECIFIED # :UNSPECIFIED
OrderState.unspecified # :unspecifiedEnums support all Enumerable methods.
OrderState.each do |key, enum|
# key and enum.key are :CREATED, :PAID
# enum.value is 'created', 'paid'
endOrderState.each_key do |key|
# :CREATED, :PAID
endOrderState.each_value do |value|
# 'created', 'paid'
endOrderState.map do |key, enum|
# key and enum.key are :CREATED, :PAID
# enum.value is 'created', 'paid'
[enum.value, key]
end
# => [ ['created', :CREATED], ['paid', :PAID] ]OrderState.reduce([]) do |arr, (key, enum)|
# key and enum.key are :CREATED, :PAID
# enum.value is 'created', 'paid'
arr [ ['created', :CREATED], ['paid', :PAID] ]OrderState.sort_by do |key, enum|
# key and enum.key are :CREATED, :PAID
# enum.value is 'created', 'paid'
enum.value.length
end
# => [[:PAID, #], [:CREATED, #]]Several hash-like methods are supported.
OrderState.keys
# => [:CREATED, :PAID]
OrderState.values
# => ['created', 'paid']OrderState.key?(:CREATED)
# => true
OrderState.value(:CREATED)
# => 'created'
OrderState.key?(:FAILED)
# => false
OrderState.value(:FAILED)
# => nilOrderState.value?('paid')
# => true
OrderState.key('paid')
# => :PAID
OrderState.value?('failed')
# => false
OrderState.key('failed')
# => nilDefining duplicate enums raises Ruby::Enum::Errors::DuplicateKeyError.
class OrderState
include Ruby::Enum
define :CREATED, 'created'
define :CREATED, 'recreated' # raises DuplicateKeyError
endDefining a duplicate value raises Ruby::Enum::Errors::DuplicateValueError.
class OrderState
include Ruby::Enum
define :CREATED, 'created'
define :RECREATED, 'created' # raises DuplicateValueError
endThe DuplicateValueError exception is raised to be consistent with the unique key constraint. Since keys are unique, there needs to be a way to map values to keys using OrderState.value('created').
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.
class OrderState
include Ruby::Enum
define :CREATED, 'CREATED'
define :PAID, 'PAID'
end
class ShippedOrderState NOTE: This will add checks at runtime which might lead to worse performance. See [benchmarks](#benchmarks).
> NOTE: `:else` is a reserved keyword if you want to use `Ruby::Enum::Case`.
```ruby
class Color -> { "order is green or blue" },
Color::YELLOW => -> { "order is yellow" },
Color::RED => -> { "order is red" },
})It also supports default/else:
color = Color::RED
Color.Case(color, {
[Color::GREEN, Color::BLUE] => -> { "order is green or blue" },
else: -> { "order is yellow or red" },
})This gem has an optional dependency to i18n. If it's available, the error messages will have a nice description and can be translated. If it's not available, the errors will only contain the message keys.
# Add this to your Gemfile if you want to have a nice error description instead of just a message key.
gem "i18n"Benchmark scripts are defined in the benchmarks folder and can be run with Rake:
rake benchmark:basic
rake benchmark:case
rake benchmark:inheritanceConstant access (e.g. Colors::RED) has no measurable overhead versus a plain Ruby constant, since it's just a constant lookup either way. Basic operations backed by a hash lookup - value, key, key?, value?, keys, values - carry some overhead (roughly 3-5x) compared to using a plain Hash directly, due to the extra method dispatch and object wrapping Ruby::Enum does internally. Run rake benchmark:basic to measure this on your own machine.
This overhead is constant regardless of how deep a subclass hierarchy is - keys, key?, value?, key, value, to_h, parse and each merge and memoize enums inherited from superclasses, so a subclass' lookups are effectively as fast as the base class' (see rake benchmark:inheritance).
The one notable exception is Ruby::Enum::Case, whose exhaustive case-like matcher is significantly slower (on the order of 50-100x, see rake benchmark:case) than a native Ruby case/when statement, since it builds and evaluates lambdas on every call rather than being optimized by the Ruby VM. Prefer a native case statement in hot code paths and reserve Ruby::Enum::Case for cases where its exhaustiveness check is worth the overhead.
For most applications this overhead is negligible in absolute terms (low single-digit microseconds per call), but it's worth being aware of in very hot code paths.
You're encouraged to contribute to ruby-enum. See CONTRIBUTING for details.
Copyright (c) 2013-2026, Daniel Doubrovkine and Contributors.
This project is licensed under the MIT License.
No open issues yet, or sync has not completed.