I’m writing a little piece of code in Ruby (1.9.3), and I use a pair of simple “enum-like” classes, that define some constants with const_set and some behavior of these constants (e.g. the class Days may have the constants MON, TUE… and Days::MON.succ should evaluate to TUE).
I’m really comfortable with these classes. However, while growing my code, i sometimes need to add more of them, and I don’t like the idea of having five or more classes that share 99% of source code, e.g.:
class Days
NAMES = %w( MON TUE ... )
INSTANCES = []
def initialize(num)
@num = num
end
# An example operation
def +(n)
INSTANCES[(@num + n) % INSTANCES.length]
end
# Another example operation
def succ
self + 1
end
def to_s
NAMES[@num]
end
NAMES.each_with_index do |name, idx|
instance = new(idx)
INSTANCES[idx] = instance
const_set name, instance
end
end
class Months
NAMES = %w( JAN FEB ... )
...
end
I was wondering if Ruby’s metaprogramming capabilities could be used to generate these classes. However, I’m having an hard time creating NAMES, INSTANCES and the “enum-named” constants (e.g. MON, TUE, …).
Being const_set a class method of Class, in this code it’s context (the value of self) is respectively Days and Months.
When creating a factory method, I’m compelled to do something like this:
def enum_new(names_array)
Class.new do
const_set "NAMES" []
names_array.each_with_index do |name, idx|
NAMES[idx] = name
end
...
end
end
Days = enum_new(%w| MON TUE ... |)
Months = enum_new(%w| JAN FEB ... |)
but this won’t work (at least, not like i hoped it to), because const_set won’t be called in the context of the class whose name is magically set (i.e. Days and Months), but apparently in the context of Class; therefore, not only it won’t be accessible from the instance methods, but it will be overwrited every time enum_new is called with a new array of names as argument. A similar problem shows up when using class variables, because they’ll be shared between any class generated with the method (because they’ll become class variables of Class, i guess).
Is there any way to create constants in a class generated with Class.new, obtaining this way classes identical in everything to the original Days and Months classes, without having to pollute my code with almost identical classes?
Thanks for your attention and patience! 🙂
Yes. Do your initialization in a
class_execblock, into which you can pass your name data and whereselfrefers to the right class: