Hi --

On Mon, 12 Sep 2005, Devin Mullins wrote:

> Yo --
>
> David A. Black wrote:
>
>> On Sun, 11 Sep 2005, Devin Mullins wrote:
>> 
>>> (I'm thinking the PickAxe may be wrong? about the private thing since you 
>>> can call #moo with an explicit receiver -- sounds more like public. Can 
>>> somebody explain?)
>> 
>> 
>> I think the private thing depends on lexical scope.  Since the nested
>> definition of moo is not lexically a top-level definition, it isn't
>> privatized.
>
> I'm not sure what you mean. Seems lexically top-level to me -- it's not 
> inside a class or module block. That it's inside a method doesn't seem to 
> make a difference:
>
> irb(main):001:0> def moo; puts 'moo' end
> => nil
> irb(main):002:0> nil.moo
> moo
> => nil

As Mark says, that's an IRB thing.  In Ruby itself:

   $ ruby -e 'def x; end; nil.x'
   -e:1: private method `x' called for nil:NilClass (NoMethodError)

And, just for reference:

   $ ruby -e 'def x; def y; p 1; end; end; x; nil.y'
   1

Since def's start a new local scope, I think it's reasonable to
describe "def y" as not being lexically top-level (which I wouldn't if
it were, say, inside an if statement rather than another def).  We can
in any case call it "outermost scope".  My interpretation is that only
methods whose definitions start in the outermost scope are made into
private methods of Object.


David

-- 
David A. Black
dblack / wobblini.net