First of all, this is not a duplicate of Enums in Ruby :)
The accepted answer of that question suggests this as a good way to represent enums in Ruby:
class Foo
BAR = 1
BAZ = 2
BIZ = 4
end
In Java it is possible to attach multiple values and methods to an enum value. I want to achive the same or something similar in Ruby.
What would be the most Ruby-like way to represent this Java enum:
public enum Enum
VALUE_1("Value 1"),
VALUE_2("Value 2"),
VALUE_3("Value 3");
Enum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
private String value;
}
EDIT:
I also want to keep the implicit features of Java enums:
- ... retrieve the ordinal value
- ... call methods on the enum values (or something equivalent)
Examples:
Enum.VALUE_1.getValue(); // "Value 1"
Enum.VALUE_2.name(); // "VALUE_2"
Enum.VALUE_3.ordinal(); // 2