Hi --

On Sat, 29 Sep 2007, Tom Werner wrote:

> This caused me a small headache the other day. By including a module, an 
> instance method suddenly becomes accessible as a module method. Seems 
> unintuitive, I'm curious to understand the reasoning behind it.
>
> module Foo
> module Bar
>   def hello
>     'hello'
>   end
> end
> end
>
> Foo::Bar.hello # => -:9: undefined method `hello' for Foo::Bar:Module 
> (NoMethodError)
>
> include Foo::Bar
>
> Foo::Bar.hello # => "hello"

At the top level, the include will include the module in Object:

ruby -e 'include Module.new; p Object.ancestors'
[Object, #<Module:0x2592c>, Kernel]

whereupon all objects will respond to its methods.


David

-- 
Upcoming training from Ruby Power and Light, LLC:
   * Intro to Ruby on Rails, Edison, NJ, October 23-26
   * Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!