Some confusion on self and the like....

A

Alexander Trauzzi

Hello all,

I'm getting somewhat confused by the use of "self" when doing Ruby
programming.

Self in other languages has typically referred to a currently executing
class, while "this" is used for the current instance.

How does it work in Ruby, given that I've seen self used to describe
static methods AND refer to the current instance?

Moreover, how do I get the furthest-down-the-inheritance-chain name of
the currently running instance?

Are there any good tutorials with examples on how to do reflection along
these lines in Ruby?

Thanks for any help!
 
M

Matt Neuburg

Alexander Trauzzi said:
Hello all,

I'm getting somewhat confused by the use of "self" when doing Ruby
programming.

Self in other languages has typically referred to a currently executing
class, while "this" is used for the current instance.

How does it work in Ruby, given that I've seen self used to describe
static methods AND refer to the current instance?

Without an example of the sort of code that is confusing you, it's hard
to be helpful, but here's a guess at what might be the difficulty you're
having.

Self is indeed the "current instance". It may be that your confusion is
caused by the fact that things in Ruby are mostly instances - even a
class is an instance (of the Class class) - and that methods can be
defined on an instance. Oh, and the fact that when you open a module or
class, the code inside the block is executable code. Thus,

class C
def self.my_method
end
end

We are opening (not "defining" - that's a misapprehension) the class C.
The line "def self.method" is therefore executable code - it is executed
when this block of code is encountered, and it means "define a method
called my_method on the instance designated by self". At this moment,
the instance designated by self is the C class, and so what we are
defining is what other languages would call a "class method".

I've written a tutorial in which I approach these matters is in what I
consider to be an understandable, Euclidean order. It might clear away
some of the cobwebs that are confusing you here. It's part of a draft of
a book on a different topic, but just ignore that fact.

http://www.apeth.com/ruby/02justenoughruby.html

m.
 
D

David A. Black

Hi --

Hello all,

I'm getting somewhat confused by the use of "self" when doing Ruby
programming.

Self in other languages has typically referred to a currently executing
class, while "this" is used for the current instance.

How does it work in Ruby, given that I've seen self used to describe
static methods AND refer to the current instance?

At any given point during execution, one object is 'self'. That object
can be of any class -- which means it can be a Class object.

For example, here:

class C
self
end

self evaluates to the class object C, which is itself an instance of
Class. Here, however:

class C
def some_method
self
end
end

self evaluates to whatever instance of C is calling some_method. In
the first example, you can figure out exactly which object self will
be by looking at the code. In the second example, you know that it
will be an instance of C but that instance doesn't exist yet.

As I've always said: the answer to 75% of all questions about Ruby is
"Because classes are objects" :)
Moreover, how do I get the furthest-down-the-inheritance-chain name of
the currently running instance?

I'm not sure what you mean. The class of self? (Which would be
self.class.)
Are there any good tutorials with examples on how to do reflection along
these lines in Ruby?

There's my new book: http://www.manning.com/black2 :)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com
 
R

Robert Klemme

I'm not sure what you mean. The class of self? (Which would be
self.class.)

It seems Alexander is asking for "self.class". Alex, you can even try
"p self.class.ancestors" to see the full inheritance chain (including
modules).

Kind regards

robert
 
R

Rick DeNatale

Hello all,

I'm getting somewhat confused by the use of "self" when doing Ruby
programming.

Self in other languages has typically referred to a currently executing
class, while "this" is used for the current instance.

Really? All of the languages I can think of use either self, this, or
_self to refer to the current instance. Some like Ruby and Smalltalk
treat Classes as objects so that in a class method the current
instance can be a class.

Which languages use self to refer to the current class.
How does it work in Ruby, given that I've seen self used to describe
static methods AND refer to the current instance?

Ruby doesn't have static methods.

def self.foo
end

defines a singleton method on the object self, which inside a class
definition is the class.

Moreover, how do I get the furthest-down-the-inheritance-chain name of
the currently running instance?

self.class will get you the class of the receiver of the currently
running method.

Not sure what you mean by name here. Objects don't have names in general.

http://talklikeaduck.denhaven2.com/2006/09/13/on-variables-values-and-objects
--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
A

Alexander Trauzzi

Robert said:
It seems Alexander is asking for "self.class". Alex, you can even try
"p self.class.ancestors" to see the full inheritance chain (including
modules).

Kind regards

robert

Indeed.

It was recently implemented in PHP as "late static binding" (as "self"
references the class in which it is used, as opposed to the context
during the invocation itself).

From what I'm reading here, "ruby does it right"(tm?)

;)

Thanks for all your help guys, I will follow up in a new topic if I have
any further questions.
 
R

Robert Klemme

It was recently implemented in PHP as "late static binding" (as "self"
references the class in which it is used, as opposed to the context
during the invocation itself).

Did you mean "defined" instead of "used"? Otherwise I cannot make much
sense of what you write.
From what I'm reading here, "ruby does it right"(tm?)

Yes - of course! :)
Thanks for all your help guys, I will follow up in a new topic if I have
any further questions.

You're very welcome.

Kind regards

robert
 
A

Alexander Trauzzi

Robert said:
Did you mean "defined" instead of "used"? Otherwise I cannot make much
sense of what you write.


Yes - of course! :)


You're very welcome.

Kind regards

robert

Just to clarify:

Simple example (language agnostic):

o Class Animal
- Method eat()
o Class Cat Extends Animal


In PHP, what would happen is if you used "self" in Animal.eat, it would
always resolve the class to be Animal (which is just silly). PHP seemed
to look up against the source file, rather than the inheritance tree.
Then, in a recent release, they introduced late static binding and a
smattering of functions and keywords for it, which opened up the
opportunities for classes to reflect on themselves after being extended.

Its still not perfect, and I can see that Ruby has made more progress in
the realm of good OO behaviour.

Hopefully that helps, just from an academic standpoint ;)
 
R

Robert Klemme

Just to clarify:

Simple example (language agnostic):

o Class Animal
- Method eat()
o Class Cat Extends Animal


In PHP, what would happen is if you used "self" in Animal.eat, it would
always resolve the class to be Animal (which is just silly). PHP seemed
to look up against the source file, rather than the inheritance tree.
Then, in a recent release, they introduced late static binding and a
smattering of functions and keywords for it, which opened up the
opportunities for classes to reflect on themselves after being extended.

PHP really seems to use "self" for the class and "this" for instances:
http://de3.php.net/manual/en/language.oop5.constants.php
Its still not perfect, and I can see that Ruby has made more progress in
the realm of good OO behaviour.

Ruby was OO from the start on - OO in Perl and PHP were retrofitted.
Hopefully that helps, just from an academic standpoint ;)

Oh, IMHO this is not just academic. These are important things to know
about a programming language.

Kind regards

robert
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top