From: Chad Fowler Date: 2004-04-13T11:32:35+09:00 Subject: Re: Two different results for Module.nesting On 12/4/2004, at 9:26 PM, why the lucky stiff wrote: > Brian Marick wrote: > >> Consider this: >> >> module M1 >> module M2 >> class C >> puts Module.nesting.inspect >> end >> end >> end >> >> It produces [M1::M2::C, M1::M2, M1], which I expected. >> >> Now try this: >> >> M1::M2::C.class_eval("Module.nesting") >> >> It produces [M1::M2::C], which I did not expect. Why the difference? >> Thanks. > > It's returning the proper nesting. But as you haven't opened each of > the Modules individually to reach the C const, Module::nesting isn't > returning them each individual. > > Let's illustrate: > > module M1::M2 > class C > puts Module.nesting.inspect > end > end > #=> [M1::M2::C, M1::M2] > > This strikes me as being a bit unintuitive, though I don't ever really need it so I'm not too bothered. :) It appears that this behavior is a remnant of the way Ruby is parsed (or, more correctly, how it's eval(.c)uated). Brian, were you using this for your automock thing? I'm interested to hear what other uses people have found for this method. Chad