# Help me understand 'self' vs current object

**URL:** <https://www.ruby-forum.com/t/help-me-understand-self-vs-current-object/167038>\
**Category:** Ruby\
**Created:** [May 2, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/help-me-understand-self-vs-current-object/167038 "2009-05-02T17:24:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@cruby](https://www.ruby-forum.com/u/cruby)\
**Post date:** [May 2, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/help-me-understand-self-vs-current-object/167038/1 "2009-05-02T17:24:27Z")

</div>

I’m pretty mystified by this, so I would appreciate any insights…  
This does involve a Rails/ActiveRecord object, but that shouldn’t matter  
here, right?

I’m archiving an object when it gets deleted, so I’m chaining methods in  
a Rails controller:  
triplet.archive(@user.id).as\_delete.save

But I checked the database and the flag never got reset from false. I  
added some print statements to as\_delete() to get this:

class ArchiveTriplet \< ActiveRecord::Base  
def as\_delete  
puts “MARKING: #{is\_deleted}”  
is\_deleted = true  
puts “MARKED: #{is\_deleted}/#{self.is\_deleted}”  
return self  
end  
end

With an additional debug after as\_delete() the log shows:  
MARKING: false  
MARKED: true/false  
ARCHIVING: #[ArchiveTriplet…, is\_deleted: false, …]

What?!

The easy solution is to change is\_deleted everywhere to self.is\_deleted,  
but am I misunderstanding what ‘self’ is supposed to be? Is there  
another keyword which means what I’ve been lead to believe ‘self’ should  
mean? (ie. ‘this’ in Java)

Thanks,

-Chris  
using: ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

---

<div class="post-metadata">

**Author:** ![cruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@cruby](https://www.ruby-forum.com/u/cruby)\
**Post date:** [May 2, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/help-me-understand-self-vs-current-object/167038/2 "2009-05-02T17:36:02Z")

</div>

On 02.05.2009 17:24, C. Dagnon wrote:

> MARKING: false  
> MARKED: true/false  
> ARCHIVING: #[ArchiveTriplet…, is\_deleted: false, …]
> 
> What?!
> 
> The easy solution is to change is\_deleted everywhere to self.is\_deleted,  
> but am I misunderstanding what ‘self’ is supposed to be? Is there  
> another keyword which means what I’ve been lead to believe ‘self’ should  
> mean? (ie. ‘this’ in Java)

You just tripped into the local variable trap. The very moment you  
placed “is\_deleted = true” in that code you told Ruby to allocate a  
local variable which is called “is\_deleted” and whose value is “true”.  
When you place the explicit receiver “self.” before “is\_deleted” Ruby  
chooses the method which accesses object state and not the local  
variable.

[http://ruby-doc.org/docs/ProgrammingRuby/html/tut\_expressions.html#S3](http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#S3)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![cruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@cruby](https://www.ruby-forum.com/u/cruby)\
**Post date:** [May 2, 2009, 7:14pm UTC](https://www.ruby-forum.com/t/help-me-understand-self-vs-current-object/167038/3 "2009-05-02T19:14:24Z")

</div>

On Saturday 02 May 2009, C. Dagnon wrote:

> |The easy solution is to change is\_deleted everywhere to self.is\_deleted

writing is\_deleted = true creates a local variable and sets it to true.  
Since  
you don’t use that variable anywhere in your method, I guess that’s not  
what  
you want. I suppose what you want is to call the is\_deleted= method  
which is  
an accessor for the @is\_deleted instance variable. If it is so, then  
changing  
is\_deleted= with self.is\_deleted is not only an “easy solution” but  
“the”  
solution.

The reason is that writing

a\_var = something

always means: create a local variable called a\_var and assign it the  
object  
something. If you want to call a method ending in = on the current  
object, you  
need to explicitly tell it to ruby, using self.

> |but am I misunderstanding what ‘self’ is supposed to be? Is there  
> |another keyword which means what I’ve been lead to believe ‘self’ should  
> |mean? (ie. ‘this’ in Java)

No, self is exactly what you think.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![cruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@cruby](https://www.ruby-forum.com/u/cruby)\
**Post date:** [May 12, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/help-me-understand-self-vs-current-object/167038/4 "2009-05-12T16:27:29Z")

</div>

Thanks for the informative responses,

I guess my polyglotism is bleeding too much. Wrong question caused by  
wrong code. Though now I have to check if since it is an ActiveRecord  
object what @is\_deleted = true does to the mix.

---

<div class="post-metadata">

**Author:** ![cruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@cruby](https://www.ruby-forum.com/u/cruby)\
**Post date:** [May 2, 2009, 10:24pm UTC](https://www.ruby-forum.com/t/help-me-understand-self-vs-current-object/167038/5 "2009-05-02T22:24:47Z")

</div>

C. Dagnon wrote:

> Help me understand ‘self’ vs current object

class A  
def initialize  
@x = 10  
end

def x=(val)  
@x = val  
end

def x  
@x  
end

def test1  
self.x = 0  
end  
end

a = A.new  
puts a.x

a.test1  
puts a.x

–output:–  
10  
0

So far so good.

class A  
def meth  
puts “hello”  
end

def test2  
meth  
end  
end

a.test2

–output:–  
hello

Bingo. When ruby sees a method call without an object in front of it,  
the implied caller is self. Inside the method test2, self is the object  
that called test2. In this case, the object ‘a’ called test2, so inside  
test2 self=a. The call to meth inside test2 has no object in front of  
it, so the implied caller is self. As a result, calling meth is  
equivalent to self.meth which is equivalent to a.meth.

Now consider this code:

class A  
def test3  
x = 0  
end  
end

a.x = 10  
a.test3  
puts a.x

What’s the output? Applying the previous lessons, ‘a’ is calling test3,  
so inside test3 self is ‘a’. The method call ‘x=’ inside test3 does not  
have an object in front of it, so the method call x= is equivalent to  
self.x=, which is equivalent to a.x=. Here’s the output:

–output:–  
10

What the??!! What happened? As the smart guys pointed out in another  
current thread, when the ruby parser first encounters the line:

x = 10

it tags it with the message, “create a variable called x and assign 10  
to it”. As a result, ruby dutifully follows the parser’s instructions  
rather than slapping ‘self’ in front of x=. In other words, an  
unadorned name with = after it causes a local variable to be created  
rather than calling the ‘name=’ method.
