# Odd 'include' behaviour?

**URL:** <https://www.ruby-forum.com/t/odd-include-behaviour/80748>\
**Category:** Ruby\
**Created:** [December 31, 2006, 12:21am UTC](https://www.ruby-forum.com/t/odd-include-behaviour/80748 "2006-12-31T00:21:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![has](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/da6949/32.png) [@has](https://www.ruby-forum.com/u/has)\
**Post date:** [December 31, 2006, 12:21am UTC](https://www.ruby-forum.com/t/odd-include-behaviour/80748/1 "2006-12-31T00:21:52Z")

</div>

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?

Thanks,

has

---

<div class="post-metadata">

**Author:** ![has](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/da6949/32.png) [@has](https://www.ruby-forum.com/u/has)\
**Post date:** [December 31, 2006, 1:41am UTC](https://www.ruby-forum.com/t/odd-include-behaviour/80748/2 "2006-12-31T01:41:00Z")

</div>

Hi –

On Sun, 31 Dec 2006, has wrote:

> end
> 
> #######
> 
> ‘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

---

<div class="post-metadata">

**Author:** ![has](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/da6949/32.png) [@has](https://www.ruby-forum.com/u/has)\
**Post date:** [December 31, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/odd-include-behaviour/80748/3 "2006-12-31T14:50:40Z")

</div>

removed\_email\_address@domain.invalid wrote:

> You’ll see Foo listed if you do:
> 
> p Object.ancestors

Okay, so if I understand correctly: main is an instance of Object. This  
means that calling include on main adds methods to every object in the  
program, including those in imported modules. However, given that:

require “somemodule”  
include SomeModule

seems to be the standard idiom for adding a module’s classes and  
methods directly to a main script, isn’t this also rather unsafe seeing  
as it breaks module encapsulation?

The problem I have is that I’m using method\_missing in various classes  
to provide a lightweight facade. This is fine as long nobody goes  
accidentally injecting extra methods into those classes (or their  
superclasses) - but if that happens then those methods are called  
instead of method\_missing, and the facade objects no longer work  
correctly.

So how can I prevent client scripts from inadvertently messing with my  
modules internals?

Thanks,

has

---

<div class="post-metadata">

**Author:** ![has](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/da6949/32.png) [@has](https://www.ruby-forum.com/u/has)\
**Post date:** [January 3, 2007, 3:06am UTC](https://www.ruby-forum.com/t/odd-include-behaviour/80748/4 "2007-01-03T03:06:55Z")

</div>

Devin M. wrote:

> has wrote:
> 
> > The problem I have is that I’m using method\_missing in various classes  
> > to provide a lightweight facade.
> > 
> > So how can I prevent client scripts from inadvertently messing with my  
> > modules internals?
> 
> [http://facets.rubyforge.org/repo/lib/facets/more/basicobject.rb](http://facets.rubyforge.org/repo/lib/facets/more/basicobject.rb)

Many thanks. Took a few hours to figure it out (it needed some changes)  
but eventually got it working and it seems to have solved the problem.

Can’t say I’m entirely comfortable with the thought of my modules  
having to put traps into Object/Module/Class though. Could other users  
have a problem with that?

has

---

<div class="post-metadata">

**Author:** ![has](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/da6949/32.png) [@has](https://www.ruby-forum.com/u/has)\
**Post date:** [December 31, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/odd-include-behaviour/80748/5 "2006-12-31T15:46:44Z")

</div>

has wrote:

> The problem I have is that I’m using method\_missing in various classes  
> to provide a lightweight facade.
> 
> So how can I prevent client scripts from inadvertently messing with my  
> modules internals?

[http://facets.rubyforge.org/repo/lib/facets/more/basicobject.rb](http://facets.rubyforge.org/repo/lib/facets/more/basicobject.rb)

Devin
