From: matt_neuburg@... (Matt Neuburg) Date: 2009-06-23T01:45:11+09:00 Subject: Re: Some confusion on self and the like.... Alexander Trauzzi 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? 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.