Created
February 6, 2015 00:41
-
-
Save wojtekmach/98e41442fe7ae28ec200 to your computer and use it in GitHub Desktop.
enum.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# >> color = Enum.new("red", "green", "blue") | |
# >> color.values | |
# => ["red", "green", "blue"] | |
# >> color["red"] | |
# => "red" | |
# >> color["yellow"] | |
# ArgumentError: Invalid value: "yellow". Allowed: "red", "green", "blue". | |
class Enum | |
def initialize(*values) | |
@values = values | |
end | |
attr_reader :values | |
def [](value) | |
unless values.include?(value) | |
raise ArgumentError, "Invalid value: #{value.inspect}. Allowed: #{values.map(&:inspect).join(", ")}." | |
end | |
value | |
end | |
end | |
if __FILE__ == $0 | |
require 'minitest/autorun' | |
begin | |
require 'minitest/doctest' | |
rescue LoadError | |
puts "minitest-doctest not found. Follow these steps until there's a gem release:" | |
puts " git clone [email protected]:lauri/minitest-doctest.git" | |
puts " cd minitest-doctest" | |
puts " rake install" | |
end | |
Minitest::Doctest.run(__FILE__) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment