I'm fairly new to Ruby metaprogramming. I'm trying to write code which generates the "dup" function for a class when it's created, using a list of fields which should be passed into the constructor. However, I can't figure out how to get access to the name of the class I'm creating, while I'm creating it.
So for example, if I had this code:
class Example
make_dup :name, :value
attr_accessor :name, :value
def initialize(name,value)
@name, @value = name, value
end
end
I'd want it to create the method:
def dup
Example.new(name,value)
end
I'm just getting stuck on how it would figure out to insert Example there.
dupmethod for the class, considering that it inheritsClass#dup. As far as obtaining the class object,selfequalsExampleas soon asclass Exampleis parsed. You can verify that with aputs.