# Everything is a object?

**URL:** <https://www.ruby-forum.com/t/everything-is-a-object/91040>\
**Category:** Ruby\
**Created:** [April 4, 2007, 12:49am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040 "2007-04-04T00:49:04Z")\
**Posts on this page:** 1\
**Page:** 3

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [April 7, 2007, 11:24am UTC](https://www.ruby-forum.com/t/everything-is-a-object/91040/41 "2007-04-07T11:24:14Z")

</div>

On Fri, Apr 06, 2007 at 10:24:58PM +0900, Jamal S. wrote:

> Now the question is,
> 
> When does the instance variable holds the object itself?

Instance variables hold references to what ever object you assigned to  
them.

> Two different objects, even if they were the same.  
> Perhaps you’d find it helpful if you tried to work out yourself what was  
> going on - and if you can’t, explain your own explanation and why you  
> think  
> it is wrong.

But to answer it for you:

(1) A.new generates a new instance of class A. Every time you invoke  
A.new  
you get a fresh object. Each object has a unique object\_id during its  
lifetime.

(2) ‘@one = A.new’ assigns (a reference to) the newly-created object  
into an  
instance variable. Since this assignment is “out in the wild”, i.e. not  
inside any “class Foo … end” or “def bar … end” construct, then you  
are  
assigning to an instance variable of a top-level session object called  
‘main’

A longer way of writing it would have been:

main.instance\_variable\_set(:@one, Foo.new)

Then assigning to @two you’ll get another object reference stored there.

Clearly then, puts @one.object\_id and puts @two.object\_id will show you  
distinct references to the two distinct objects you created.

Try also:

puts main.inspect  
puts main.instance\_variables

However unless you’re just playing around in irb, nobody creates  
instance  
variables inside ‘main’. You normally create them within an object of  
your  
own.

Brian.

[Previous page](https://www.ruby-forum.com/t/everything-is-a-object/91040.md?page=2)
