# Self, looking for a good summary explanation

**URL:** <https://www.ruby-forum.com/t/self-looking-for-a-good-summary-explanation/99428>\
**Category:** Ruby\
**Created:** [July 22, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/self-looking-for-a-good-summary-explanation/99428 "2006-07-22T18:01:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 22, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/self-looking-for-a-good-summary-explanation/99428/1 "2006-07-22T18:01:10Z")

</div>

Sorry, because I have the books and have / continuing to read, just  
trying to wrap myself around one aspect of “self”.

My understanding is that ‘self’ changes amongst objects depending on  
program code execution. This I get (at some level). what I don’t  
understand is why are some objects named self outright and does it  
actually matter if you named something self or something else ?

Hope this question makes sense.

TIA  
Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 22, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/self-looking-for-a-good-summary-explanation/99428/2 "2006-07-22T22:15:28Z")

</div>

On Jul 22, 2006, at 11:59 AM, Dark A. wrote:

> My understanding is that ‘self’ changes amongst objects depending on  
> program code execution. This I get (at some level). what I don’t  
> understand is why are some objects named self outright and does it  
> actually matter if you named something self or something else ?

Objects aren’t really ‘named’ by the variables that reference them.  
Consider:

def outer  
a = 3  
puts a # 3  
inner  
puts a # 3  
end

def inner  
a = 4  
puts a # 4  
end

outer # call method outer

Both methods have a variable ‘a’, but the scope is different in each  
case so they are in fact different variables existing in two different  
scopes but utilizing the same name. In this case the ‘a’ in outer  
references the Fixnum object 3 and the ‘a’ in inner references a  
different  
object, the Fixnum object 4.

The same concept applies to ‘self’. Ruby arranges for the identifier  
‘self’ to reference different objects depending on the scope in which  
execution is occurring. So consider:

class A  
def instance\_method\_of\_A  
self.object\_id  
end  
end

a1 = A.new  
a2 = A.new

a1.instance\_method\_of\_A # =\> 1656984  
a2.instance\_method\_of\_A # =\> 1650774

You can see from this example that ‘self’ within an instance method  
references the object that is receiving the message. In the first  
case the object referenced as a1 is the receiver and self.object\_id  
returns the object id of that object but in the second case a2 is  
the receiver and self.object\_id returns a different object id.

The rules regarding scopes, and the object associated with ‘self’ in  
those various scopes, are part of what makes Ruby, well, Ruby. So  
understanding the various scopes and their associated variables is  
the key to understanding Ruby code.

Gary W.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 22, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/self-looking-for-a-good-summary-explanation/99428/3 "2006-07-22T23:41:11Z")

</div>

On 2006-07-22, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sorry, because I have the books and have / continuing to read, just  
> trying to wrap myself around one aspect of “self”.
> 
> My understanding is that ‘self’ changes amongst objects depending on  
> program code execution. This I get (at some level). what I don’t  
> understand is why are some objects named self outright and does it  
> actually matter if you named something self or something else ?

self is not a variable name - It’s a key word that refers to the current  
object (the object to which the currently executing code in question  
belongs). It’s often not needed - e.g.:

class A

```
def b
	# ...
end

def c
	self.b
end

def d
	b
end

```

end

both ‘c’ and ‘d’ do exactly the same thing, call b on the current  
object,  
using different syntax. However, sometimes self is necessary - e.g.:

x.rb:

class X  
def method1(some\_object)  
some\_object.do\_something\_or\_other(self) # Pass my’self’ - me, the  
# current object as an  
arg.  
end  
# …  
end

y.rb:

require ‘x’

class Y  
def do\_work  
x = X.new  
x.method1(self) # Again, pass my’self’.  
end

```
def do_something_or_other(client)
	puts "doing something with #{client}"
end

```

end

test-y:

#!/usr/bin/env ruby

require ‘y’

y = Y.new  
y.do\_work

Note that ‘self’ is used twice in the above example. (This is the only  
way, as far as I know, to pass a reference to “the current object” in  
these method calls, so ‘self’ is a necessary keyword. All OO languages  
have such a keyword - e.g., ‘this’ in C++ and Java, ‘Current’ in Eiffel,  
‘self’ in Python, Ruby, and, I think, Smalltalk.) It doesn’t do  
anything  
useful, since it’s just a demo, but a serious program that uses ‘self’  
will need it to do something useful, of course.
