Class::Enum
'Classial' class enum for Ruby
class Animal
include Class::Enum
attr_reader :sound
def initialize(sound)
@sound = sound
end
field :CAT, "meow"
field :DOG, "bark"
field :BIRD, "tweet"
end
puts Animal::CAT.sound # meow
case Animal::CAT
when Animal
puts "An animal"
else
puts "X"
end
# An animal
Animal::CAT.instance_of?(Animal) # => true
Animal::CAT.class # => Animal
Requirements
- No
Usage
See documentation
Examples
Using block
class Animal
include Class::Enum
attr_reader :block
def initialize(&block)
@block = block
end
field :CAT do
puts "meow"
end
end
Animal::CAT.block.call # meow
Using keyword arguments
You can keyword arguments only if ruby 2.0+. If not ruby 2.0+, this gem doesn't use keyword arguments
class Animal
include Class::Enum
attr_reader :sound
def initialize(sound:)
@sound = sound
end
field :CAT, sound: "meow"
end
puts Animal::CAT.sound # meow
Author
Sputnik Gugja ([email protected])
License
This is free software released into the public domain (CC0 license).
See LICENSE file.