# Using 'include' but not poluting my namespace

**URL:** <https://www.ruby-forum.com/t/using-include-but-not-poluting-my-namespace/79626>\
**Category:** Ruby\
**Created:** [March 10, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/using-include-but-not-poluting-my-namespace/79626 "2006-03-10T20:55:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![exile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/50afbb/32.png) [@exile](https://www.ruby-forum.com/u/exile)\
**Post date:** [March 10, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/using-include-but-not-poluting-my-namespace/79626/1 "2006-03-10T20:55:24Z")

</div>

I noticed that if you ‘include’ a module within a method, it will polute  
the  
namesapce that defines that method. I found a simple solution is to  
declare a  
throw-away class, and execute my code in that. Is there a more better  
way to  
achieve this?

# Pollutes current namespace

def init()  
include Inertia

do\_stuff\_with\_inertia()  
end

# Doesn’t polute namespace, but ugly

class DontPolute  
include Inertia

do\_stuff\_with\_inertia()  
end

Mike

---

<div class="post-metadata">

**Author:** ![exile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/50afbb/32.png) [@exile](https://www.ruby-forum.com/u/exile)\
**Post date:** [March 10, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/using-include-but-not-poluting-my-namespace/79626/2 "2006-03-10T21:13:39Z")

</div>

On Sat, 11 Mar 2006, Mike A. wrote:

> end

both pollute your namespace equally. the first pollutes class Object,  
the  
second class DontPolute. remember that you’re code in ruby, if not  
inside a  
class, is inside the Object class. this allows you to pretend your  
coding  
something like perl. i always write main programs like so

class Main  
…  
end

Main::new if $0 == **FILE**

to avoid any such issues.

if you are looking for a workaround you can also use

Module::new{ include Inertia … }

regards.

-a

---

<div class="post-metadata">

**Author:** ![exile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/50afbb/32.png) [@exile](https://www.ruby-forum.com/u/exile)\
**Post date:** [March 10, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/using-include-but-not-poluting-my-namespace/79626/3 "2006-03-10T21:56:22Z")

</div>

On Friday 10 March 2006 13:53, Mike A. wrote:

> end
> 
> # Doesn’t polute namespace, but ugly
> 
> class DontPolute  
> include Inertia
> 
> do\_stuff\_with\_inertia()  
> end
> 
> Mike

Instead of include, you may want:  
require ‘intertia’

This will load up inertia.rb, which i’m assuming contains either a class  
or a  
module name Intertia. Then you can use it via Interia.new() or  
Intertia::do\_stuff\_with\_intertia().

Correct me if I’m misunderstanding what you’re trying to do here.
