Uri Agassi's answer is for setting instance variables on the class itself, something similar to, but not the same as class variables. See Difference between class variables and class instance variables? for an explantation of the differences.
What you're looking for is something like rails' cattr_accessor
. You can achieve that with a bit of meta-programming
module CattrAccessors
def cattr_accessor(*attrs)
cattr_reader(*attrs)
cattr_writer(*attrs)
end
def cattr_reader(*attrs)
attrs.each do |attr|
define_singleton_method(attr) { class_variable_get("@@#{attr}") }
end
end
def cattr_writer(*attrs)
attrs.each do |attr|
define_singleton_method("#{attr}=") { |value| class_variable_set("@@#{attr}", value) }
end
end
end
Then use it like so:
class School
extend CattrAccessors
attr_accessor :syllabus
end
I haven't tested the above module, so it may not work. Let me know if it does not.