Newbie: Equivalent to "this" keyword in Java?

If I am writing a method and I want to refer to the instance of the
object itself, do I use “self”? Can I use “this”? Is “this” an alias
for “self”?

Wes

Wes G. wrote:

If I am writing a method and I want to refer to the instance of the
object itself, do I use “self”? Can I use “this”? Is “this” an alias
for “self”?

It’s self. There’s no alias, but you can say

this = self

if you want… :wink:

Hal

On 3/27/06, Wes G. [email protected] wrote:

If I am writing a method and I want to refer to the instance of the
object itself, do I use “self”? Can I use “this”? Is “this” an alias
for “self”?

It’s “self”, and it can also be used to differentiate between local
variables and methods, in the (very rare, usually Rails-based)
situations where there can be ambiguity.

Got it. I was auto-creating a variable named “this” before :).

Wilson B. wrote:

On 3/27/06, Wes G. [email protected] wrote:

If I am writing a method and I want to refer to the instance of the
object itself, do I use “self”? Can I use “this”? Is “this” an alias
for “self”?

It’s “self”, and it can also be used to differentiate between local
variables and methods, in the (very rare, usually Rails-based)
situations where there can be ambiguity.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs