Methods such as attr_accessor and has_many are often called "mock methods" because they look like ruby keywords (mimic them), but in fact they, like you and others correctly pointed out, are method calls.
dd = DooDad.new dd.foo
prints nil and never spits out any puts things
How exactly does it all work?
When you are inside the class definition, the implicit recipient of all method calls and "variable definitions" is self , which is DooDad in your case.
So when you write
class DooDad @foo = 1 end
you actually define the instance variable on self , which turns out to be the class itself, since you are inside the definition of these classes. (and outside the definition of any other class, module, or method)
The attr_accessor method, on the other hand, generates metaprogramming access methods for an instance variable of objects that are created from the DooDad class .
Back to your example:
class DooDad attr_accessor :foo puts "I happened!" @foo = 7 end
Based on the foregoing, you should understand that you are dealing with two different @foo variables, one for instances of the DooDad class (i.e. DooDad.new ), the other (the one you created by writing @foo = 7 ) for the class itself DooDad!
When you call the new method on a class, you instantiate it.
dd = DooDad.new
The puts "I happened!" statement puts "I happened!" , like the other two, it is actually evaluated immediately after loading the class, but not when calling new on it. If you want you to describe the behavior (by doing things when calling new ), I suggest implementing the initialize() method for DooDad , which is called when calling new :
class DooDad attr_accessor :foo def initialize() puts "I happened!" @foo = 7 end end dd = DooDad.new
But why is @foo = 7 now setting the dd instance variable instead of DooDad ? When you define a method with the def keyword, you enter a new area (you pass the area shutter). self is no longer a class, but instead an instance of this class that you created with new , just like dd . Therefore, when you write @foo = 7 inside a method definition, you are talking about variables for an instance of the DooDad class, and not about the class itself.
This post is probably too long and may not even satisfy the quality of the answer, but I hope it was somewhat comprehensive.