0

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Rich
  • 153
  • 7
  • Though interesting, I feel there is more than one question being asked here. – Sagar Pandya Dec 12 '16 at 22:17
  • I think it's a lot more question than the OP wants an answer for. It's one of those things that's good to accept as "it just works", until farther up the learning curve. – the Tin Man Dec 12 '16 at 22:49
  • I'd suggest reading this possible duplicate http://stackoverflow.com/q/4370960/128421 – the Tin Man Dec 12 '16 at 22:50
  • Its been a while, the missing link was in @theTinMan post, that says Ruby classes are evaluated and that is when the `attr_accessor` is invoked. – Rich Jul 25 '17 at 17:30

1 Answers1

0

Nothing gets attached anywhere. attr_accessor is simply defined more or less like this:

class Module
  def attr_accessor(*names)
    names.each do |name|
      define_method(name)        do       instance_variable_get(:"@#{name}"     )
      define_method(:"#{name}=") do |val| instance_variable_set(:"@#{name}", val)
    end
  end
end

That's all there is to it.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653