Ruby::Enum

Build Status

Enum-like behavior for Ruby, heavily inspired by this and improved upon another blog post.

Usage

class Colors
  include Ruby::Enum

  define :RED, "red"
  define :GREEN, "green"
end

Referencing

Colors::RED # "red"
Colors::GREEN # "green"
Colors::UNDEFINED # raises Ruby::Enum::Errors::UninitializedConstantError
Colors.keys # [ :RED, :GREEN ]
Colors.values # [ "red", "green" ]
Colors.to_h # { :RED => "red", :GREEN => "green" }

Iterating

Colors.each do |key, enum|
  # key and enum.key is :RED, :GREEN
  # enum.value is "red", "green"
end

Mapping

Colors.map do |key, enum|
  # key and enum.key is :RED, :GREEN
  # enum.value is "red", "green"
  [enum.value, key]
end

# => [ ['red', :RED], ['green', :GREEN] ]

Contributing

You're encouraged to contribute to this gem.

  • Fork this project.
  • Make changes, write tests.
  • Updated CHANGELOG.
  • Make a pull request, bonus points for topic branches.

Copyright (c) 2013, Daniel Doubrovkine and Contributors.

This project is licensed under the MIT License.