I have a class that should look something like this:
class Family_Type1
@people = Array.new(3)
@people[0] = Policeman.new('Peter', 0)
@people[1] = Accountant.new('Paul', 0)
@people[2] = Policeman.new('Mary', 0)
def initialize(*ages)
for i in 0 ... @people.length
@people[i].age = ages[i]
end
end
end
I want to be able to define a bunch of classes similar to this one at runtime (define them once at startup) where the size of the array and the type assigned to each parameter is defined at runtime from an external specification file.
I sort of got it to work using evals but this is really ugly. Any better way?
Best Solution
From what I understand, you need meta-programming. Here is a snippet of code for creating classes dynamically (on the fly) with initialize method that initializes instance variables-
Hope you can tweak it to suit your requirements.