R
Rajinder Yadav
I am just starting to expand my Ruby knowledge into the area of
meta-programming.
I want to be able to create a class dynamically. Lets call it, class
Person, and then I want to add methods to it dynamically.
From a static point I have managed to compile the following example:
class Person
def self.extend_me
class_eval "def greet; puts 'hello'; end"
instance_eval "def name; puts 'Person'; end"
end
end
Person.extend_me
Person.respond_to? :greet
Person.respond_to? :name
puts Person.name
p = Person.new
puts p.greet
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?
--
Kind Regards,
Rajinder Yadav
http://DevMentor.org
Do Good! - Share Freely
meta-programming.
I want to be able to create a class dynamically. Lets call it, class
Person, and then I want to add methods to it dynamically.
From a static point I have managed to compile the following example:
class Person
def self.extend_me
class_eval "def greet; puts 'hello'; end"
instance_eval "def name; puts 'Person'; end"
end
end
Person.extend_me
Person.respond_to? :greet
Person.respond_to? :name
puts Person.name
p = Person.new
puts p.greet
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?
--
Kind Regards,
Rajinder Yadav
http://DevMentor.org
Do Good! - Share Freely