Self, looking for a good summary explanation

D

Dark Ambient

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
 
G

gwtmp01

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 Wright
 
J

Jim Cochrane

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top