I'm writing Ruby code that creates new classes using Object.const_set, which works great for creating new classes and instantiating instances of them. But I'd like these new classes to inherit from a class I've hardcoded already. I can't find methods to do this. Here's my code:
def create_model_class(klass_name, klass_vars)
klass = Object.const_set(klass_name, Class.new)
klass.class_eval do
define_method(:initialize)
klass_vars.each_with_index do |name, i|
instance_variable_set("@"+name[i], name[i])
end
end
end
end
doafterdefine_methodhas gone awol. It appears thatklass_varsis a two-dimensional array. Is that intended? If klass_vars is an just array of strings, I think you need to changeeach_with_indextoeachand drop the reference toi. RE @Sergios suggestion, I believe you would addparent_classto the arguments and add(parent_class)toClass.newor addparent_class_nameto the arguments and(parent_class_name.to_sym)toClass.new.do.