# Are these the same?

**URL:** <https://rubytalk.org/t/are-these-the-same/18958>\
**Category:** ruby-talk\
**Created:** [14 June 2005 00:05 UTC](https://rubytalk.org/t/are-these-the-same/18958 "2005-06-14T00:05:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lowell\_Kirsh](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Lowell\_Kirsh](https://rubytalk.org/u/Lowell_Kirsh)\
**Post date:** [14 June 2005 00:05 UTC](https://rubytalk.org/t/are-these-the-same/18958/1 "2005-06-14T00:05:30Z")

</div>

I'm wondering about the difference between writing something like

@x = ...  
and  
self.x = ...

from within a class. Are they the same thing? To be more concrete, I have given a fuller example below:

class Foo  
&nbsp;&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.x = 1  
&nbsp;&nbsp;&nbsp;end  
end

class Foo  
&nbsp;&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@x = 1  
&nbsp;&nbsp;&nbsp;end  
end

Lowell

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [14 June 2005 00:32 UTC](https://rubytalk.org/t/are-these-the-same/18958/2 "2005-06-14T00:32:40Z")

</div>

Lowell Kirsh wrote:

> I'm wondering about the difference between writing something like
> 
> @x = ...  
> and  
> self.x = ...
> 
> from within a class. Are they the same thing? To be more concrete, I have given a fuller example below:

They are different, but the degree of difference depends on the context. The "@x =" construct is ruby's built-in way of assigning to an instance variable in the current object ("self"). It does not invoke any methods. The "self.x =" construct is a special syntax for calling the method whose name is "x=" on the object denoted by "self". This method may or may not assign to the instance variable @x, and it may do other things in addition to assigning to @x. For instance, you might also execute some debugging code:

class Foo  
&nbsp;&nbsp;&nbsp;def x=(val)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "setting x = #{val.inspect}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@x = val  
&nbsp;&nbsp;&nbsp;end  
end

Or you might not assign to @x at all. For example:

class Square  
&nbsp;&nbsp;&nbsp;def width=(val)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.height = val  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;def height=(val)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@height = val  
&nbsp;&nbsp;&nbsp;end  
end

Since width and height must be equal, there's no need to store them separately. But users of the Square class don't have to know that. Some people call this the "uniform access principle".

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [14 June 2005 00:38 UTC](https://rubytalk.org/t/are-these-the-same/18958/3 "2005-06-14T00:38:10Z")

</div>

Lowell Kirsh wrote:

> I'm wondering about the difference between writing something like
> 
> @x = ...  
> and  
> self.x = ...
> 
> from within a class. Are they the same thing?

Not necessarily the same.

@x = ... # assigns an instance variable  
self.x = ... # invokes a method called x= (which doesn't  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# exist in your example.

Now, having said they're different... if you add

&nbsp;&nbsp;&nbsp;attr\_accessor :x

then they are more the same.

Bear in mind that instance variables are accessed from the  
"outside world" only via methods.

And when you say self.x within a class, you are basically  
accessing it "from the outside" even though you are inside  
the class (dumb analogy: You're in your house, and you  
reach outside a window and then reach into the window next  
to it "from outside").

Also bear in mind that if you (manually) define a method x=,  
you \*could\* define it in a stupid or misleading way (just  
as you \*could\* give misleading names to all your variables  
and methods):

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def x=(val)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Ha, fooled you! I'm setting @y to 123!"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@y = 123 # and not even using val  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

Hope this helps...

Hal

---

<div class="post-metadata">

**Author:** ![Lowell\_Kirsh](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Lowell\_Kirsh](https://rubytalk.org/u/Lowell_Kirsh)\
**Post date:** [14 June 2005 04:15 UTC](https://rubytalk.org/t/are-these-the-same/18958/4 "2005-06-14T04:15:30Z")

</div>

Thank you both. Things are much clearer now 🙂

Lowell Kirsh wrote:

> **···**
>
> > I'm wondering about the difference between writing something like
> > 
> > @x = ...  
> > and  
> > self.x = ...
> > 
> > from within a class. Are they the same thing? To be more concrete, I have given a fuller example below:
> > 
> > class Foo  
> > &nbsp;&nbsp;def bar  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.x = 1  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Foo  
> > &nbsp;&nbsp;def bar  
> > &nbsp;&nbsp;&nbsp;&nbsp;@x = 1  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Lowell
