Hi --

On Sun, 31 Dec 2006, has wrote:

> Wondering if the following behaviour is a bug or a feature:
>
> #######
>
> module Foo
> 	class Bar
> 	end
>
> 	def baz
> 	end
> end
>
> o = Foo::Bar.new
> p respond_to?(:baz) # false
> p o.respond_to?(:baz) # false
>
> include Foo
>
> o = Bar.new
> p respond_to?(:baz) # true
> p o.respond_to?(:baz) # true <------ shouldn't this be false?
>
> #######
>
> 'include Foo' adds Bar and baz to the top-level namespace as expected.
> What I don't understand is why baz should also appear as a method of
> Bar. Anyone know what's going on here?

Try adding this:

   p "hi".respond_to?(:baz)  # true

By including Foo at the top level, you've mixed it into Object.  So
every object now includes Foo in its method lookup path.

You'll see Foo listed if you do:

   p Object.ancestors


David

-- 
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)