Instances variables belong to objects (aka instances), that's why they are called "instance variables" after all.
There are two objects here: Test
, which is an instance of Class
, and a
, which is an instance of Test
. Both are objects just like any other object. Both can have instance variables just like any other object.
Both have an instance variable called @a
. Foo
's @a
has been initialized to 100
. a
's @a
hasn't been initialized at all, and unitialized instance variables evaluate to nil
.
So, your problem is that you are confusing which instance you are looking at. Instance variables are always looked up in self
, and inside a class definition body, self
is the class being defined.
This may sound pedantic, but I find that understanding that Ruby is actually much simpler than people sometimes want you to believe, will ultimately help you.