Help me understand this code please

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?

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs