I'm trying to create a new class, without knowing the name of the class until it's supposed to på created.
Something like this;
variable = "ValidClassName"
class variable
end
Test = ValidClassName.new
If possible, i'd also appreciate som hints on how to dynamically add attributes (and methods) to this new class.
I'll be retreiving 'settings' for the class, and they will look something like this:
title :Person
attribute :name, String
attribute :age, Fixnum
But should not be designed to accept only that explicit file, the attributes might differ in number end type.
Which in the end will generate a class that should look something like:
class Person
def initialize(name, age)
@name_out = name
@age_out = age
end
end
Help?
A class gains its name when it is assigned to a constant. So It's easy to do in a generic fashion with
const_set
.For example, let's say you want to use
Struct
to build a class with some attributes, you can:To inherit from another class, replace the
Struct.new
byClass.new(MyBaseClass)
, say:Your code would look something akin to this:
...or you could just use eval: