Some confusion on self and the like

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!

···

--
Posted via http://www.ruby-forum.com/.

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.

···

Alexander Trauzzi <atrauzzi@gmail.com> wrote:

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?

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" :slight_smile:

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: The Well-Grounded Rubyist :slight_smile:

David

···

On Tue, 23 Jun 2009, Alexander Trauzzi wrote:

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

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

···

On Mon, Jun 22, 2009 at 12:15 PM, Alexander Trauzzi<atrauzzi@gmail.com> wrote:
--
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

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

···

On 22.06.2009 18:46, David A. Black wrote:

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.)

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

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.)

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?)

:wink:

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

···

On 22.06.2009 18:46, David A. Black wrote:

--
Posted via http://www.ruby-forum.com/\.

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! :slight_smile:

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

···

On 22.06.2009 21:22, Alexander Trauzzi wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

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! :slight_smile:

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

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 :wink:

···

On 22.06.2009 21:22, Alexander Trauzzi wrote:

--
Posted via http://www.ruby-forum.com/\.

*** correction: Change Animal.eat to instanceOfCat.eat()

:wink:

···

--
Posted via http://www.ruby-forum.com/.

Robert Klemme wrote:

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! :slight_smile:

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

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 :wink:

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

Kind regards

  robert

···

On 22.06.2009 22:25, Alexander Trauzzi wrote:

On 22.06.2009 21:22, Alexander Trauzzi wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/