Glenn Smith
3/29/2005 11:30:00 AM
Ah. That's what confused me. I couldn't find it in pickaxe2.
Thanks David(s!)
Glenn
On Tue, 29 Mar 2005 20:11:01 +0900, David A. Black <dblack@wobblini.net> wrote:
> Hello --
>
> On Tue, 29 Mar 2005, Glenn Smith wrote:
>
> > Not entirely sure I understand this (it's a newbie-ruby question).
> >
> > class Test
> > def foo
> > end
> >
> > def Test.foo
> > end
> >
> > def self.foo
> > end
> > end
> >
> >
> > The first definitition of foo is an instance method. The second a
> > class method. Perhaps my terminology is wrong but I understand what I
> > mean.
> >
> > It's the third one I'm not sure of. The "self.foo".
> >
> > What does this do, how and where would I use it?
>
> Every time you do this:
>
> def some_object.some_method
> ...
> end
>
> you create a singleton method some_method for the object some_object
> -- that is, a method that only some_object can call.
>
> If you do the above using 'self' as the receiver, then the singleton
> method you create will belong to whatever 'self' was at the time.
>
> In your example, self is actually Test, the class whose scope you are
> in. So, in effect, Test.foo and self.foo are the same, in that
> context.
>
> David
>
> --
> David A. Black
> dblack@wobblini.net
>
>
--
All the best
Glenn
Aylesbury, UK