How would I declared a, 'class Person' dynamically and then add methods and attributes to it? Can someone point me to good documentation on this or show me some simple code example?
How would I declared a, 'class Person' dynamically
Option 1: since you're using string eval already, you could just do the
same.
eval "class Person; end"
Option 2:
Person = Class.new # superclass is Object
Person = Class.new(Mammal) # superclass is Mammal
I find this isn't often done in practice though. A program which doesn't
know in advance which classes it has can be a bit too dynamic You
would probably register your classes somewhere to be able to find them.
In a Hash is one option; under a Module is another, so you can use
MyModule.constants
to find them all.
You can make your classes anonymous if you don't bind them to a
constant:
my_klass = Class.new
and then add methods and attributes to it?
As you've done before, using string eval, is one way.
If you want to define the class dynamically, take a look at Class.new.
This creates an anonymous class that you can assign to a constant
directly or with const_set.
To define methods I would use define_method. Adding attributes is done
adding methods that set and get the attributes:
irb(main):042:0> C = Class.new do
irb(main):043:1* def self.add_method(name, &blk)
irb(main):044:2> define_method(name, &blk)
irb(main):045:2> end
irb(main):046:1> end
=> C
How would I declared a, 'class Person' dynamically and then add methods and
attributes to it? Can someone point me to good documentation on this or show
me some simple code example?
How would I declared a, 'class Person' dynamically and then add methods and
attributes to it? Can someone point me to good documentation on this or show
me some simple code example?
If you want to define the class dynamically, take a look at Class.new.
This creates an anonymous class that you can assign to a constant
directly or with const_set.
To define methods I would use define_method. Adding attributes is done
adding methods that set and get the attributes:
irb(main):042:0> C = Class.new do
irb(main):043:1* def self.add_method(name, &blk)
irb(main):044:2> define_method(name, &blk)
irb(main):045:2> end
irb(main):046:1> end
=> C
How would I declared a, 'class Person' dynamically
Option 1: since you're using string eval already, you could just do the same.
eval "class Person; end"
Option 2:
Person = Class.new # superclass is Object
Person = Class.new(Mammal) # superclass is Mammal
I find this isn't often done in practice though. A program which doesn't know in advance which classes it has can be a bit too dynamic You would probably register your classes somewhere to be able to find them. In a Hash is one option; under a Module is another, so you can use
MyModule.constants
to find them all.
You can make your classes anonymous if you don't bind them to a constant:
my_klass = Class.new
and then add methods and attributes to it?
As you've done before, using string eval, is one way.
Of course, since 'name' is static here, you could just include a module.
It gets more fun when the names of the accessors themselves are dynamic.
At this point it really does make more sense to use string eval, rather
than the alternative (instance_variable_get and instance_variable_set).
Have a look at define_method_attribute= in the following:
Also see define_read_method in attribute_methods/read.rb, and
method_missing in base.rb (this defines finder methods dynamically)
Of course, since 'name' is static here, you could just include a module.
It gets more fun when the names of the accessors themselves are dynamic. At this point it really does make more sense to use string eval, rather than the alternative (instance_variable_get and instance_variable_set).