If self refers to the object created by instantiating its current class. Why does self in this case equal a? We instantiated twice? We have 2 objects?
on 2016-09-24 15:23
on 2016-09-29 11:53
Your code is incomplete, and I don't quite understand all of your questions. I assume that the first class (where you did not provide the full code) is the class Animal. We have three instanciations. First, a Zebra is instanciated and assigned to a. Second, another Zebra is instanciated and assigned to b. Then, the method + is invoked on a. Inside the method body, self is bound to receiver, which is the object a. Hence. self.name means the name of the Zebra a. During the execution of +, a Animal is instantiated and returned as a result of the + method. This Animal is then passed to puts.