# Difference instance variable @ or self.

**URL:** <https://discuss.rubyonrails.org/t/difference-instance-variable-or-self/662>\
**Category:** rubyonrails-talk\
**Created:** [August 28, 2006, 8:47pm UTC](https://discuss.rubyonrails.org/t/difference-instance-variable-or-self/662 "2006-08-28T20:47:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_Leitch](https://avatars.discourse-cdn.com/v4/letter/i/e79b87/32.png) [@Ian\_Leitch](https://discuss.rubyonrails.org/u/Ian_Leitch)\
**Post date:** [August 28, 2006, 8:47pm UTC](https://discuss.rubyonrails.org/t/difference-instance-variable-or-self/662/1 "2006-08-28T20:47:25Z")

</div>

I’m pretty new to Ruby myself, I’ve kinda learned as required so I’m patchy.

From what I’ve seen however, I think ‘self’ is relative to the current scope, i.e instance or class. Where as @ is always in instance scope.

Ian.

---

<div class="post-metadata">

**Author:** ![Jason\_Roelofs](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Jason\_Roelofs](https://discuss.rubyonrails.org/u/Jason_Roelofs)\
**Post date:** [August 28, 2006, 9:18pm UTC](https://discuss.rubyonrails.org/t/difference-instance-variable-or-self/662/2 "2006-08-28T21:18:42Z")

</div>

self is the object itself.

[self.name](http://self.name) is saying that there’s a public method on self to be called (attr\_reader :name)

@name says ‘get the instance variable @name for this class instance’

Use ‘self’ pretty much the same way you’d use ‘this’ in Java or C++, though not in the cases that you can use @ (passing yourself to another method is the most used example).

Jason

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://discuss.rubyonrails.org/u/David_A_Black1)\
**Post date:** [August 28, 2006, 9:58pm UTC](https://discuss.rubyonrails.org/t/difference-instance-variable-or-self/662/3 "2006-08-28T21:58:59Z")

</div>

Hi --

> > > First: I'm new to Ruby
> > > 
> > > Simple model:
> > > 
> > > class Product \< ActiveRecord::Base
> > > 
> > > &nbsp;&nbsp;def save &nbsp;&nbsp;&nbsp;&nbsp;STDERR \<\< "save: " \<\< @name \<\< "\n" &nbsp;&nbsp;&nbsp;&nbsp;STDERR \<\< "save: " \<\< self.name \<\< "\n" &nbsp;&nbsp;end end
> > > 
> > > I thought that both lines should give the same result...
> > 
> > I'm pretty new to Ruby myself, I've kinda learned as required so I'm patchy.
> > 
> > From what I've seen however, I think 'self' is relative to the current scope, i.e instance or class. Where as @ is always in instance scope.

self is the "default object". There's always one and only one self at any point in a Ruby program.

Since classes are objects, self can be a class:

&nbsp;&nbsp;&nbsp;class SelfTest &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p self &nbsp;&nbsp;&nbsp;end # =\> output: SelfTest

Instance variables are a way for individual objects (including Class objects) to store information and maintain state. Every object has its own instance variables.

There's a tight connection between instance variables and self: whenever you see @var, @name, etc., you're seeing an instance variable that belongs to self.

> self is the object itself.
> 
> self.name is saying that there's a public method on self to be called (attr\_reader :name)

All it really says is that you're sending the message 'name' to the object self. Usually you do that in cases where there's a corresponding method -- but not always.

attr\_reader isn't connected to this; there are many methods you can call on objects that aren't created with attr-reader.

> @name says 'get the instance variable @name for this class instance'

More precisely: @name is the instance variable @name belonging to self (whatever self is at that given moment in runtime).

David
