In the docs for Class it says:
class Name
# some code describing the class behavior
end
When a new Class is created, an object of type Class is initialized and assigned to a global constant (Name in this case).
When Name.new is called to create a new object, the new method in Class is run by default.
There is also this diagram
+---------+ +-...
| | |
BasicObject-----|-->(BasicObject)-------|-...
^ | ^ |
| | | |
Object---------|----->(Object)---------|-...
^ | ^ |
| | | |
+-------+ | +--------+ |
| | | | | |
| Module-|---------|--->(Module)-|-...
| ^ | | ^ |
| | | | | |
| Class-|---------|---->(Class)-|-...
| ^ | | ^ |
| +---+ | +----+
| |
obj--->OtherClass---------->(OtherClass)-----------...
If I create an instance of Name:
class Name
attr_accessor :foo
# some code describing the class behavior
end
n = Name.new
How is it that the object n
ends up with the method n.foo
?
At first I thought attr_accessor
was a method of type Object but looking at the docs it is actually of Module and I don't see anywhere that says Module is mixed in to Object.
Then I thought that since Class inherits from Module and when Name.new
is called, which I assume new
is a method on the initialized object of type Class, it will create an instance of Name and also call attr_accessor
in the Module? But the docs for attr_accessor
say,
Defines a named attribute for this module
Does that mean there is also a module created at the same time? How does it then relate to my Name instance?