# Method self.a= and a= create different instance variables

**URL:** <https://www.ruby-forum.com/t/method-self-a-and-a-create-different-instance-variables/99826>\
**Category:** Ruby\
**Created:** [July 25, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/method-self-a-and-a-create-different-instance-variables/99826 "2006-07-25T20:21:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuesefa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9f8e36/32.png) [@yuesefa](https://www.ruby-forum.com/u/yuesefa)\
**Post date:** [July 25, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/method-self-a-and-a-create-different-instance-variables/99826/1 "2006-07-25T20:21:40Z")

</div>

i try this in irb .

irb(main):001:0\> def a=(a)  
irb(main):002:1\> @a=a  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> def a  
irb(main):005:1\> @a  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> a=1  
=\> 1  
irb(main):008:0\> a  
=\> 1  
irb(main):009:0\> self.a=2  
=\> 2  
irb(main):010:0\> a  
=\> 1  
irb(main):011:0\> self.a  
=\> 2

a and a= should be private methods of Object. so call of a or self.a  
should do the same thing. but it’s not. i am now confused.

---

<div class="post-metadata">

**Author:** ![yuesefa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9f8e36/32.png) [@yuesefa](https://www.ruby-forum.com/u/yuesefa)\
**Post date:** [July 25, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/method-self-a-and-a-create-different-instance-variables/99826/2 "2006-07-25T20:47:31Z")

</div>

yuesefa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> irb(main):007:0\> a=1  
> =\> 1  
> irb(main):008:0\> a  
> =\> 1

irb(main):010:0\> @a  
=\> nil  
irb(main):011:0\> self.a  
=\> nil  
That means that not the method a= was called, but the local variable a  
was set  
to one.

> irb(main):009:0\> self.a=2  
> =\> 2

Here you expicitly call the method, so @a is set to 2.

> irb(main):010:0\> a  
> =\> 1

But the local variable a is still 1.

> irb(main):011:0\> self.a  
> =\> 2

Here the method a is explicitly called, so you get what you expected.

> a and a= should be private methods of Object. so call of a or self.a  
> should do the same thing. but it’s not. i am now confused.

It’s simply because the ruby-interpreter can’t tell what you want when  
you  
write a=1 – do you want to set a to 1, or do you want to call the  
method a?

HTH,  
Tom

---

<div class="post-metadata">

**Author:** ![yuesefa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9f8e36/32.png) [@yuesefa](https://www.ruby-forum.com/u/yuesefa)\
**Post date:** [July 25, 2006, 8:53pm UTC](https://www.ruby-forum.com/t/method-self-a-and-a-create-different-instance-variables/99826/3 "2006-07-25T20:53:57Z")

</div>

thank both of u, i am clear now 🙂

---

<div class="post-metadata">

**Author:** ![yuesefa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9f8e36/32.png) [@yuesefa](https://www.ruby-forum.com/u/yuesefa)\
**Post date:** [July 25, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/method-self-a-and-a-create-different-instance-variables/99826/4 "2006-07-25T20:44:20Z")

</div>

Hi –

On Wed, 26 Jul 2006, yuesefa wrote:

> irb(main):007:0\> a=1  
> a and a= should be private methods of Object. so call of a or self.a  
> should do the same thing. but it’s not. i am now confused.

When you write:

a = 1

Ruby parses that as a local variable assignment. In other words,  
methods ending in = always require that you provide an explicit  
receiver (in this case “self”).

David

---

<div class="post-metadata">

**Author:** ![yuesefa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9f8e36/32.png) [@yuesefa](https://www.ruby-forum.com/u/yuesefa)\
**Post date:** [July 25, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/method-self-a-and-a-create-different-instance-variables/99826/5 "2006-07-25T21:09:44Z")

</div>

On 7/25/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > irb(main):004:0\> def a  
> > =\> 1  
> > Ruby parses that as a local variable assignment. In other words,  
> > methods ending in = always require that you provide an explicit  
> > receiver (in this case “self”).

also,

@a

will give you the same as

self.a

which makes sense because you are “inside” the definition of Object:

irb(main):085:0\> self  
=\> #\<Object:0x277fa2c @a=7\>

Les
