From: Adam Prescott Date: 2011-02-03T08:39:50+09:00 Subject: Re: Amazing behavior of include module --0016e6dee83416b70f049b552a54 Content-Type: text/plain; charset=UTF-8 On Wed, Feb 2, 2011 at 11:15 PM, Andrew Wagner wrote: > This is really quite odd to me. Especially since AAA::BBB.constants returns > an empty array. Anybody have an answer to this? > > On Wed, Feb 2, 2011 at 11:49 AM, Pavel Strnad >wrote: > > > Hi, > > I do not understand how is following code working: > > irb(main):001:0> module AAA > > irb(main):002:1> class BBB > > irb(main):003:2> end > > irb(main):004:1> end > > => nil > > irb(main):005:0> BBB::BBB > > NameError: uninitialized constant BBB > > from (irb):5 > > from /usr/local/bin/irb:12:in `
' > > irb(main):006:0> AAA::BBB::BBB > > NameError: uninitialized constant AAA::BBB::BBB > > from (irb):6 > > from /usr/local/bin/irb:12:in `
' > > irb(main):007:0> include AAA > > => Object > > irb(main):008:0> BBB::BBB > > => AAA::BBB > > irb(main):009:0> BBB::BBB::BBB > > => AAA::BBB > > irb(main):010:0> BBB::BBB::BBB::BBB > > => AAA::BBB > > irb(main):011:0> AAA::BBB::BBB::BBB > > => AAA::BBB > > > > I thought that last four lines will not work, but they do. How is it > > possible? > > Thank you. > > Pavel Strnad > > > > > Including AAA at the top level, as in line 7, acts the same as including it on Object, so every object suddenly has BBB available to it. module A; class B; end; end include A Object::A Fixnum::A class X; end X::A X.ancestors # => [X, Object, A, Kernel, BasicObject] --0016e6dee83416b70f049b552a54--