From: Robert Klemme Date: 2011-02-03T18:48:38+09:00 Subject: Re: Amazing behavior of include module On Thu, Feb 3, 2011 at 5:01 AM, Andrew Wagner wrote: >> >> 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. > > This explanation makes sense, and certainly fits the facts. But why is doing > 'include' from an irb prompt the same as including the module in Object? If > self points to main, and main is an instance of Object, shouldn't the module > get included into main's eigenclass? I believe IRB mimics behavior of the Ruby interpreter here: 10:44:58 ~$ ruby19 < module A; class B; end; end > include A > p B > p Fixnum::B > CODE A::B A::B 10:45:32 ~$ ruby19 -e 'p self' main 10:45:51 ~$ irb19 Ruby version 1.9.2 irb(main):001:0> self => main Reasoning behind this is certainly is that this way namespaces work as expected. If only main would get access to the inclusion, you could not use a constant nested in your included module in *any* other class. Personally I don't like including at top level that much because it always includes the risk of name clashes. I usually do this only in small or one off scripts. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/