I have to write a my_initialize method in class Class, so that it works in this manor, when used in another class:
class Person
my_initialize :name, :surname
end
is equivalent to :
class Person
def initialize(name, surname)
@name, @surname = name, surname
end
end
It also has to raise an ArgumentError if a wrong number of arguments is passed. For example Person.new("Mickey") is invalid. I know that my code should look something like:
class Class
def my_initialize(*args)
args.each do |arg|
self.class_eval("?????")
end
end
end
I just started to read metaprogramming, but can't find anything useful for my problem. Any ideas how to do this task?
define_method. :)