Module: Ruby::Enum::Case
- Defined in:
- lib/ruby-enum/enum/case.rb
Overview
Adds a method to an enum class that allows for exhaustive matching on a value.
class Color
include Ruby::Enum
include Ruby::Enum::Case
define :RED, :red
define :GREEN, :green
define :BLUE, :blue
define :YELLOW, :yellow
end
Color.case(Color::RED, {
[Color::RED, Color::GREEN] => -> { "red or green" },
Color::BLUE => -> { "blue" },
Color::YELLOW => -> { "yellow" },
})
Reserves the :else key for a default case: Color.case(Color::RED, {
[Color::RED, Color::GREEN] => -> { "red or green" },
else: -> { "blue or yellow" },
})
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Class Method Details
.included(klass) ⇒ Object
31 32 33 |
# File 'lib/ruby-enum/enum/case.rb', line 31 def self.included(klass) klass.extend(ClassMethods) end |