hemant
12/12/2006 12:03:00 AM
On 12/12/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Tue, 12 Dec 2006, hemant wrote:
>
> > ##Won't work method #1
> >
> > class Foobar
> > private
> > def foo; p "Foobar"; end
> > end
> >
> > class Baz < Foobar
> > def blah; foo; end
> > end
> >
> > baz = Baz.new
> > baz.foo
> >
> > ## Work method #2
> >
> > class Foobar
> > private
> > def foo; p "Foobar"; end
> > end
> >
> > class Baz < Foobar
> > def blah; foo; end
> > end
> >
> > baz = Baz.new
> > baz.blah
> >
> > PickAxe2:
> >
> > If a method is private, it may be called only within the context of
> > the calling object—it is never possible to access another object's
> > private methods directly, even if the object is of the same class as
> > the caller.
> >
> > So what happens in Method#2, why foo can be easily called, when called
> > from inside the class Baz? I mean, that method foo should be still
> > bound to instance baz...so in which context foo gets called in
> > Method#2.
>
> "In the context of" an object means: "when 'self' is that object". In
> the first example, your call to foo takes place when baz is not self.
> In the second example, inside blah, self is indeed baz, so it's
> allowed to call foo.
>
So can i rephrase this as, private methods can't be called from
outside class definition, because self is not bound to anything
outside the class. If my assertion is correct, then ok..I get the
idea.
Thanks
--
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.