# Problem with 'self'. behaves different on setting and gettin

**URL:** <https://rubytalk.org/t/problem-with-self-behaves-different-on-setting-and-gettin/42634>\
**Category:** ruby-talk\
**Created:** [30 November 2007 13:45 UTC](https://rubytalk.org/t/problem-with-self-behaves-different-on-setting-and-gettin/42634 "2007-11-30T13:45:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal\_Gabrukiewicz](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Michal\_Gabrukiewicz](https://rubytalk.org/u/Michal_Gabrukiewicz)\
**Post date:** [30 November 2007 13:45 UTC](https://rubytalk.org/t/problem-with-self-behaves-different-on-setting-and-gettin/42634/1 "2007-11-30T13:45:38Z")

</div>

hello,  
i have a question according 'self'. I have setup a small simple scenario  
to simulate the problem.

This following small program works fine ..

> class User  
> &nbsp;&nbsp;def set\_foo  
> &nbsp;&nbsp;&nbsp;&nbsp;self.x = '1'  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def get\_foo  
> &nbsp;&nbsp;&nbsp;&nbsp;x  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def x=(val)  
> &nbsp;&nbsp;&nbsp;&nbsp;@x = val  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def x  
> &nbsp;&nbsp;&nbsp;&nbsp;@x  
> &nbsp;&nbsp;end  
> end
> 
> u = User.new  
> u.set\_foo  
> puts(u.x)  
> puts(u.get\_foo)

when i now remove the 'self' within the method 'set\_foo' it does not set  
the values any more.

> class User  
> &nbsp;&nbsp;def set\_foo  
> &nbsp;&nbsp;&nbsp;&nbsp;x = '1'  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def get\_foo  
> &nbsp;&nbsp;&nbsp;&nbsp;x  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def x=(val)  
> &nbsp;&nbsp;&nbsp;&nbsp;@x = val  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def x  
> &nbsp;&nbsp;&nbsp;&nbsp;@x  
> &nbsp;&nbsp;end  
> end
> 
> u = User.new  
> u.set\_foo  
> puts(u.x)  
> puts(u.get\_foo)

so my 1st question is: why doesnt the 'set\_foo' method calls the 'x'  
method if i remove the self?  
THE INTERESTING THING: 'get\_foo' works with 'x' and 'self.x'

and the 2nd question: when should i use self now and when not and why is  
there this strange behavoir?

thanks for looking into this.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jari\_Williamsson](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jari\_Williamsson](https://rubytalk.org/u/Jari_Williamsson)\
**Post date:** [30 November 2007 14:33 UTC](https://rubytalk.org/t/problem-with-self-behaves-different-on-setting-and-gettin/42634/2 "2007-11-30T14:33:48Z")

</div>

Michal Gabrukiewicz wrote:

> so my 1st question is: why doesnt the 'set\_foo' method calls the 'x'  
> method if i remove the self?

Because it now thinks that x is a local variable. This is one of the basics of Ruby.

If you use NetBeans, the editor will alert you about these kind of things in the colored syntax.

> THE INTERESTING THING: 'get\_foo' works with 'x' and 'self.x'

Yes, but with different result behind the scenes. 'x' is the same as 'x()', while 'self.x' is the same a '@x'

> and the 2nd question: when should i use self now and when not and why is  
> there this strange behavoir?

You should use self when a left-side argument can be interpreted as a local variable.

Best regards,

Jari Williamsson

---

<div class="post-metadata">

**Author:** ![Michal\_Gabrukiewicz](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Michal\_Gabrukiewicz](https://rubytalk.org/u/Michal_Gabrukiewicz)\
**Post date:** [30 November 2007 15:04 UTC](https://rubytalk.org/t/problem-with-self-behaves-different-on-setting-and-gettin/42634/3 "2007-11-30T15:04:10Z")

</div>

thanks jari ... i can see clearly now 🙂

Jari Williamsson wrote:

> Michal Gabrukiewicz wrote:
> 
> > so my 1st question is: why doesnt the 'set\_foo' method calls the 'x'  
> > method if i remove the self?
> 
> Because it now thinks that x is a local variable. This is one of the  
> basics of Ruby.

thats the important thing!

> **···**
>
> > If you use NetBeans, the editor will alert you about these kind of  
> > things in the colored syntax.
> > 
> > > THE INTERESTING THING: 'get\_foo' works with 'x' and 'self.x'
> > 
> > Yes, but with different result behind the scenes. 'x' is the same as  
> > 'x()', while 'self.x' is the same a '@x'
> > 
> > > and the 2nd question: when should i use self now and when not and why is  
> > > there this strange behavoir?
> > 
> > You should use self when a left-side argument can be interpreted as a  
> > local variable.
> > 
> > Best regards,
> > 
> > Jari Williamsson
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![HiddenBek](https://avatars.discourse-cdn.com/v4/letter/h/bc79bd/32.png) [@HiddenBek](https://rubytalk.org/u/HiddenBek)\
**Post date:** [30 November 2007 15:15 UTC](https://rubytalk.org/t/problem-with-self-behaves-different-on-setting-and-gettin/42634/4 "2007-11-30T15:15:02Z")

</div>

Here's a small example to illustrate Jari's point:

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

&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;self.x = 1  
&nbsp;&nbsp;&nbsp;&nbsp;puts x # =\> 1  
&nbsp;&nbsp;&nbsp;&nbsp;x = 2  
&nbsp;&nbsp;&nbsp;&nbsp;puts x # =\> 2  
&nbsp;&nbsp;&nbsp;&nbsp;puts self.x # =\> 1  
&nbsp;&nbsp;end  
end

User.new.foo

Any assignment without an explicit receiver will set a local  
variable. You also need to use self if the method you're calling is  
shadowed by a local variable with the same name, as in my example  
above. Finally, you must use self if the method you're calling is a  
reserved word, such as self.class.

Page 86 of the Pickaxe book (Programming Ruby) talks about this.

> **···**
>
> On Nov 30, 10:33 am, Jari Williamsson \<jari.williams...@mailbox.swipnet.se\> wrote:
> 
> > Michal Gabrukiewicz wrote:  
> > \> so my 1st question is: why doesnt the 'set\_foo' method calls the 'x'  
> > \> method if i remove the self?
> > 
> > Because it now thinks that x is a local variable. This is one of the  
> > basics of Ruby.
> > 
> > If you use NetBeans, the editor will alert you about these kind of  
> > things in the colored syntax.
> > 
> > \> THE INTERESTING THING: 'get\_foo' works with 'x' and 'self.x'
> > 
> > Yes, but with different result behind the scenes. 'x' is the same as  
> > 'x()', while 'self.x' is the same a '@x'
> > 
> > \> and the 2nd question: when should i use self now and when not and why is  
> > \> there this strange behavoir?
> > 
> > You should use self when a left-side argument can be interpreted as a  
> > local variable.
> > 
> > Best regards,
> > 
> > Jari Williamsson

---

<div class="post-metadata">

**Author:** ![Claudio\_Caseiro](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Claudio\_Caseiro](https://rubytalk.org/u/Claudio_Caseiro)\
**Post date:** [30 November 2007 15:40 UTC](https://rubytalk.org/t/problem-with-self-behaves-different-on-setting-and-gettin/42634/5 "2007-11-30T15:40:30Z")

</div>

Jari Williamsson wrote:

> Michal Gabrukiewicz wrote:
> 
> > THE INTERESTING THING: 'get\_foo' works with 'x' and 'self.x'
> 
> Yes, but with different result behind the scenes. 'x' is the same as  
> 'x()', while 'self.x' is the same a '@x'

I think this is not correct. Since there are no local variable "x" in  
method get\_foo, bot "x" and "self.x" will send message x to self.

If you reopen class User:

class User  
&nbsp;&nbsp;def x  
&nbsp;&nbsp;&nbsp;&nbsp;"2"  
&nbsp;&nbsp;end  
end

Both x and self.x in get\_foo will return "2", even if @x is "1"

Regards,

> **···**
>
> --  
> Cláudio Caseiro
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Daniel\_Martin](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@Daniel\_Martin](https://rubytalk.org/u/Daniel_Martin)\
**Post date:** [1 December 2007 22:05 UTC](https://rubytalk.org/t/problem-with-self-behaves-different-on-setting-and-gettin/42634/6 "2007-12-01T22:05:39Z")

</div>

One minor correction to an otherwise perfectly fine explanation:

Jari Williamsson \<jari.williamsson@mailbox.swipnet.se\> writes:

> Yes, but with different result behind the scenes. 'x' is the same as  
> 'x()', while 'self.x' is the same a '@x'

Not quite. self.x is the same as x(), but:

self.x = ...

is the same as self.x=(...). The connection to @x only happens  
because of the definition for the x= method in the orginal post.

> **···**
>
> --  
> s=%q( Daniel Martin -- martin@snowplow.org  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "s=%q(#{s})",s.to\_a.last )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "s=%q(#{s})",s.to\_a.last

---

<div class="post-metadata">

**Author:** ![Charles\_Oliver\_Nutte](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@Charles\_Oliver\_Nutte](https://rubytalk.org/u/Charles_Oliver_Nutte)\
**Post date:** [30 November 2007 15:47 UTC](https://rubytalk.org/t/problem-with-self-behaves-different-on-setting-and-gettin/42634/7 "2007-11-30T15:47:09Z")

</div>

Cláudio Caseiro wrote:

> Jari Williamsson wrote:
> 
> > Michal Gabrukiewicz wrote:
> > 
> > > THE INTERESTING THING: 'get\_foo' works with 'x' and 'self.x'
> > 
> > Yes, but with different result behind the scenes. 'x' is the same as  
> > 'x()', while 'self.x' is the same a '@x'
> 
> I think this is not correct. Since there are no local variable "x" in method get\_foo, bot "x" and "self.x" will send message x to self.
> 
> If you reopen class User:
> 
> class User  
> &nbsp;&nbsp;def x  
> &nbsp;&nbsp;&nbsp;&nbsp;"2"  
> &nbsp;&nbsp;end  
> end
> 
> Both x and self.x in get\_foo will return "2", even if @x is "1"

You are correct. Both are calls to the "x" method. The only difference is that "self.x" will fail if "x" is private.

- Charlie
