# Subject: What \*exactly\* does an instance variable in the top

**URL:** <https://www.ruby-forum.com/t/subject-what-exactly-does-an-instance-variable-in-the-top/112229>\
**Category:** Ruby\
**Created:** [August 16, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/subject-what-exactly-does-an-instance-variable-in-the-top/112229 "2007-08-16T17:11:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [August 16, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/subject-what-exactly-does-an-instance-variable-in-the-top/112229/1 "2007-08-16T17:11:25Z")

</div>

Here’s an example:

* * *

@tl\_instance = “hello ruby”  
puts @tl\_instance

def tl\_method  
puts “from tl\_method – @tl\_instance: #{@tl\_instance}”  
end

tl\_method

class Tl\_class  
def tlc\_method  
puts “from tlc\_method – @tl\_instance: #{@tl\_instance}”  
end  
end

## Tl\_class.new.tlc\_method

In tlc\_method, I am unable to access @tl\_instance. Presumably because  
it’s an instance variable in some other class not Tl\_class. But who’s  
instance variable is it? Kernel? If so, how do I access it?

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [August 16, 2007, 5:19pm UTC](https://www.ruby-forum.com/t/subject-what-exactly-does-an-instance-variable-in-the-top/112229/2 "2007-08-16T17:19:26Z")

</div>

On Fri, 2007-08-17 at 00:09 +0900, Pito S. wrote:

> tl\_method  
> In tlc\_method, I am unable to access @tl\_instance. Presumably because  
> it’s an instance variable in some other class not Tl\_class. But who’s  
> instance variable is it? Kernel? If so, how do I access it?

celtic@sohma:~$ ruby  
puts “Self is #{self}”  
puts “Self is of #{self.class}”  
@i = 32  
puts self.instance\_variable\_get(’@i’)  
puts instance\_variable\_get(’@i’)  
Self is main  
Self is of Object  
32  
32  
celtic@sohma:~$

You get the same results if you enter these in irb. The scope is an  
Object called `main’, and all instance variables belong to it – not,  
say, a class like Kernel.

Cheers,  
Arlen.

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [August 16, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/subject-what-exactly-does-an-instance-variable-in-the-top/112229/3 "2007-08-16T17:42:32Z")

</div>

Any instance variable refers to the object currently being self at the  
moment.

> @tl\_instance = “hello ruby”

self here is “main”, which is the top level default object

> puts @tl\_instance
> 
> def tl\_method  
> puts “from tl\_method – @tl\_instance: #{@tl\_instance}”  
> end

self here is “main” too

> tl\_method

=\> Working as expected

> class Tl\_class  
> def tlc\_method  
> puts “from tlc\_method – @tl\_instance: #{@tl\_instance}”  
> end  
> end
> 
> ## Tl\_class.new.tlc\_method

Inside tlc.method, self is the “Tl\_class.new” object, which doesn’t know  
about any @tl\_instance.
