Brian Candler
5/4/2007 5:48:00 AM
On Fri, May 04, 2007 at 01:52:21PM +0900, Lucas Holland wrote:
> receiver). So, in this instance, that's the case. When I however create
> a class like so:
>
> class MyClass
> private
> def test
> puts "hello, world"
> end
> public
> def pub
> test
> end
> end
>
> I am able to call the test method because of the implicit receiver,
> self. In this case, however, self refers to the class MyClass (which is
> an instance of Class, as far as I know).
No. Inside your 'test' method, which is an instance method of MyClass, self
refers to an object which is one particular instance of MyClass.
That is, you can't run it like this:
MyClass.pub
Rather, you have to do
MyClass.new.pub
(the receiver of the message is an *instance* of MyClass that you've
created)
Outside of 'def test', self does indeed refer to the class.
class MyClass
p self # self is 'MyClass'
def test
p self # self is an instance of MyClass
end
end