# Requiring a file into a different namespace

**URL:** <https://www.ruby-forum.com/t/requiring-a-file-into-a-different-namespace/62769>\
**Category:** Ruby\
**Created:** [June 7, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/requiring-a-file-into-a-different-namespace/62769 "2006-06-07T23:19:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [June 7, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/requiring-a-file-into-a-different-namespace/62769/1 "2006-06-07T23:19:03Z")

</div>

[“a”,“b”].each do |name|  
sandbox = Module.new { require “#{name}.rb” }

```
survey = sandbox::Survey.new

puts survey.name

```

end

This is what I’d like to do, but ‘name’ is out of scope and when I  
hardcode it, it seems like require is pulling class Survey from the file  
into the global namespace rather than the anonymous module’s namespace.  
It’s weird.

Anyone have a solution for requiring a file into a different namespace?

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [June 7, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/requiring-a-file-into-a-different-namespace/62769/2 "2006-06-07T23:46:01Z")

</div>

On Jun 7, 2006, at 5:19 PM, Michael J. wrote:

> hardcode it, it seems like require is pulling class Survey from the

Search the mailing list archives, this was discussed very recently.

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [June 8, 2006, 1:53am UTC](https://www.ruby-forum.com/t/requiring-a-file-into-a-different-namespace/62769/3 "2006-06-08T01:53:36Z")

</div>

Use #load. Not tested but it will be something like:

[“a”,“b”].each do |name|  
sandbox = Module.new { load “#{name}.rb” }

```
survey = sandbox::Survey.new

puts survey.name

```

end

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [June 8, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/requiring-a-file-into-a-different-namespace/62769/4 "2006-06-08T19:58:47Z")

</div>

Jacob F. wrote:

> Unfortunately, this won’t work. You can’t even do this directly:

Ah crud. I actually meant:

module\_eval File.read("#{name}.rb")

(I’ve made that booboo twice now!) Sorry about that.

T.

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [June 8, 2006, 4:10am UTC](https://www.ruby-forum.com/t/requiring-a-file-into-a-different-namespace/62769/5 "2006-06-08T04:10:11Z")

</div>

Michael J. wrote:

> hardcode it, it seems like require is pulling class Survey from the file  
> into the global namespace rather than the anonymous module’s namespace.  
> It’s weird.

It’s not weird. Require (or load) is going to load the script normally  
and return true or false; that line is (except for order of execution)  
the same as this:

x = require “#{name}.rb”  
sandbox = Module.new { x }

If what you want is for the script’s code to be evaluated in the  
context of the module, you need to use module\_eval, as seen in the  
latest ruby-dev summary:

sandbox = Module.new  
sandbox.module\_eval File.read("#{name}.rb")

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [June 8, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/requiring-a-file-into-a-different-namespace/62769/6 "2006-06-08T19:22:38Z")

</div>

On 6/7/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Use #load. Not tested but it will be something like:
> 
> [“a”,“b”].each do |name|  
> sandbox = Module.new { load “#{name}.rb” }
> 
> ```
> survey = sandbox::Survey.new
> 
> puts survey.name
> 
> ```
> 
> end

Unfortunately, this won’t work. You can’t even do this directly:

$ cat a.rb  
sandbox = Module.new{ module Survey; end }  
p sandbox::Survey rescue puts “sandbox has no Survey”  
p Survey rescue puts “there is no top level Survey”

$ ruby a.rb  
sandbox has no Survey  
Survey

So, you can see, the Survey module is defined at the top level, rather  
than inside the anonymous sandbox. The same thing happens for  
module\_eval, instance\_eval and class\_eval in block form. However, in  
string form, module\_eval does the trick:

$ cat b.rb  
sandbox = Module.new  
sandbox.module\_eval “module Survey; end”  
p sandbox::Survey rescue puts “sandbox has no Survey”  
p Survey rescue puts “there is no top level Survey”

$ ruby b.rb  
#Module:0x402a3cc0::Survey  
there is no top level Survey

This is simply enough extended for getting the string to be eval’d via  
File.read:

$ cat source.rb  
module Survey; end

$ cat target.rb  
sandbox = Module.new  
sandbox.module\_eval File.read(“source.rb”)  
p sandbox::Survey rescue puts “sandbox has no Survey”  
p Survey rescue puts “there is no top level Survey”

$ ruby target.rb  
#Module:0x402a3bf8::Survey  
there is no top level Survey

What I really want to know is this: Kernel#load allows a parameter  
that wraps the loaded file in an anonymous module, so as not to  
pollute the global namespace. But that anonymous module is discarded  
– as far as I can tell, the only use for this style of load is if the  
only portion of the file you’re interested in is the side effects.

I suggest one of two changes.

1. Change Kernel#load to return the anonymous module when the wrap  
parameter is true. Currently, the return value of Kernel#load is  
always true (failure results in an exception), so there should be no  
code relying on the current return value.

2. Since the wrap parameter can only be true or false right now,  
change that parameter to accept a module. The behavior when that  
parameter is false/nil/true is unchanged for backwards compatibility,  
but if the parameter is a module, load the file into that module  
instead of into a new anonymous module.

How do these ideas sound to people?

Jacob F.

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [June 8, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/requiring-a-file-into-a-different-namespace/62769/7 "2006-06-08T20:02:18Z")

</div>

Jacob F. wrote:

> How do these ideas sound to people?

Sounds good. Although I think the most recent develpoment “trial” is  
#require\_into.

T.

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [June 9, 2006, 4:05am UTC](https://www.ruby-forum.com/t/requiring-a-file-into-a-different-namespace/62769/8 "2006-06-09T04:05:16Z")

</div>

Jacob F. wrote:  
…

> always true (failure results in an exception), so there should be no  
> code relying on the current return value.

Until then, [http://raa.ruby-lang.org/project/script](http://raa.ruby-lang.org/project/script) may be useful.
