# Nesting Includes

**URL:** <https://www.ruby-forum.com/t/nesting-includes/51621>\
**Category:** Ruby\
**Created:** [November 15, 2005, 2:08am UTC](https://www.ruby-forum.com/t/nesting-includes/51621 "2005-11-15T02:08:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![listrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f17d59/32.png) [@listrecv](https://www.ruby-forum.com/u/listrecv)\
**Post date:** [November 15, 2005, 2:08am UTC](https://www.ruby-forum.com/t/nesting-includes/51621/1 "2005-11-15T02:08:57Z")

</div>

Hi. I’d like to include one module, which would include other modules  
into my namespace. Is this doable?

Doing this:  
----file1.rb  
module AAA  
include bbb  
end

----file2.rb  
include AAA

does not seem to include bbb. Is there anyway to do this, or do I need  
to add the include explicilty to file2 as well?

(Note - I understand the need for this, to avoid namespace collisions).

---

<div class="post-metadata">

**Author:** ![listrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f17d59/32.png) [@listrecv](https://www.ruby-forum.com/u/listrecv)\
**Post date:** [November 15, 2005, 3:42am UTC](https://www.ruby-forum.com/t/nesting-includes/51621/2 "2005-11-15T03:42:11Z")

</div>

On 11/15/05, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> include AAA
> 
> does not seem to include bbb. Is there anyway to do this, or do I need  
> to add the include explicilty to file2 as well?

Hi, you need to require the files you want:

----bbb.rb  
module BBB  
def bbb  
puts “bbb”  
end  
end

----aaa.rb  
require ‘bbb’

module AAA  
include BBB  
end

---- test-nested-includes.rb  
require ‘aaa’  
include AAA  
bbb

---- OUTPUT  
bbb

Regards,

Sean

---

<div class="post-metadata">

**Author:** ![listrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f17d59/32.png) [@listrecv](https://www.ruby-forum.com/u/listrecv)\
**Post date:** [November 15, 2005, 4:06am UTC](https://www.ruby-forum.com/t/nesting-includes/51621/3 "2005-11-15T04:06:14Z")

</div>

On Nov 14, 2005, at 8:07 PM, removed\_email\_address@domain.invalid wrote:

> does not seem to include bbb. Is there anyway to do this, or do I  
> need  
> to add the include explicilty to file2 as well?

It seems like you are thinking (in the way that I did while learning  
Ruby)  
that Ruby’s ‘include’ facility is analogous to the C/C++ include  
mechanism.  
Same keyword but totally unrelated semantics.

As Sean indicated, use ‘require’ to have ruby parse a file and use  
‘include’  
to mixin modules (i.e. to dynamically construct the hierarchy of  
modules/classes).  
You have to make sure that Ruby has parsed a module definition via  
‘require’ before  
it will be available for use via ‘include’. If your modules were all  
defined  
in a single file, it would look like:

module B

# definition of module B

end

module A  
include B

# more parts of A

end

include A

So if B is defined in ‘file1.rb’ and A is defined in ‘file2.rb’ you  
could  
access those definitions in a third file as:

```
require 'file1' # now B is defined, for use by A
require 'file2' # now A is defined, using B

include A # module A is included into the current scope (top level)

```

Gary W.
