In particular, I would like to be able to perform logical tests against the set of values โโgiven by a variable. An example would be the state of a window: "minimized, maximized, closed, open"
If you need enumerations to map to values โโ(for example, you need to minimize equal to 0, maximum equal to 100, etc.), I would use a hash of characters for the values, for example:
WINDOW_STATES = { :minimized => 0, :maximized => 100 }.freeze
Freezing (for example, says Nate) will prevent you from accidentally hacking things in the future. You can check if this is really done by doing this.
WINDOW_STATES.keys.include?(window_state)
Alternatively, if you don't need any values, and you just need to check the โmembershipโ then the array will be good
WINDOW_STATES = [:minimized, :maximized].freeze
Use it like this:
WINDOW_STATES.include?(window_state)
If your keys are strings (for example, the status field in a RoR application), you can use an array of strings. I do this ALL TIME in many of our rail applications.
WINDOW_STATES = %w(minimized maximized open closed).freeze
This is pretty much what validates_inclusion_of validator rails is for :-)
Personal note:
I don't like typing? all the time, so I have this (this only gets complicated due to the .in? case (1, 2, 3):
class Object
This allows you to enter
window_state.in? WINDOW_STATES