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

**URL:** <https://www.ruby-forum.com/t/problem-with-self-behaves-different-on-setting-and-gettin/121749>\
**Category:** Ruby\
**Created:** [November 30, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/problem-with-self-behaves-different-on-setting-and-gettin/121749 "2007-11-30T14:45:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 30, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/problem-with-self-behaves-different-on-setting-and-gettin/121749/1 "2007-11-30T14:45:41Z")

</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  
> def set\_foo  
> self.x = ‘1’  
> end  
> def get\_foo  
> x  
> end  
> def x=(val)  
> @x = val  
> end  
> def x  
> @x  
> 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  
> def set\_foo  
> x = ‘1’  
> end  
> def get\_foo  
> x  
> end  
> def x=(val)  
> @x = val  
> end  
> def x  
> @x  
> 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.

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 30, 2007, 3:34pm UTC](https://www.ruby-forum.com/t/problem-with-self-behaves-different-on-setting-and-gettin/121749/2 "2007-11-30T15:34:42Z")

</div>

Michal G. 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 W.

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 30, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/problem-with-self-behaves-different-on-setting-and-gettin/121749/3 "2007-11-30T16:04:14Z")

</div>

thanks jari … i can see clearly now 🙂

Jari W. wrote:

> Michal G. 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 W.

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 30, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/problem-with-self-behaves-different-on-setting-and-gettin/121749/4 "2007-11-30T16:15:43Z")

</div>

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

class User  
attr\_accessor :x

def foo  
self.x = 1  
puts x # =\> 1  
x = 2  
puts x # =\> 2  
puts self.x # =\> 1  
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 W.

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 30, 2007, 4:40pm UTC](https://www.ruby-forum.com/t/problem-with-self-behaves-different-on-setting-and-gettin/121749/5 "2007-11-30T16:40:33Z")

</div>

Jari W. wrote:

> Michal G. 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  
def x  
“2”  
end  
end

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

## Regards,

ClÃ¡udio Caseiro

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 1, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/problem-with-self-behaves-different-on-setting-and-gettin/121749/6 "2007-12-01T23:06:21Z")

</div>

One minor correction to an otherwise perfectly fine explanation:

Jari W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 30, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/problem-with-self-behaves-different-on-setting-and-gettin/121749/7 "2007-11-30T16:47:56Z")

</div>

ClÃ¡udio Caseiro wrote:

> method get\_foo, bot “x” and “self.x” will send message x to self.
> 
> If you reopen class User:
> 
> class User  
> def x  
> “2”  
> 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
