I'm reading Metaprogramming Ruby and just want to clear something up about the following paraphrased code:
class MyClazz
def self.my_class_method(name)
define_method(name) {
# do stuff
}
end
my_class_method :foo
my_class_method :bar
end
# The code above generates instance methods:
# def foo
# do stuff
# end
# def bar
# do stuff
# end
Q1
My first question regards the two method calls at the end of the file: my_class_method :foo
and my_class_method :bar
. Am I right in thinking that they are both invoked automatically when a MyClazz object is instantiated?
Q2 When Ruby generates these methods (def foo
and def bar
), it will put them in MyClazz's eigenclass, even though they are instance methods. So does this mean that Ruby looks to the eigenclass for both class and instance methods when needed?
I just want to clear that up before I move too far into the book.