# "load"ing a file and keeping the current scope?

**URL:** <https://www.ruby-forum.com/t/load-ing-a-file-and-keeping-the-current-scope/94605>\
**Category:** Ruby\
**Created:** [May 8, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/load-ing-a-file-and-keeping-the-current-scope/94605 "2006-05-08T22:08:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [May 8, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/load-ing-a-file-and-keeping-the-current-scope/94605/1 "2006-05-08T22:08:25Z")

</div>

Hi -

For a variety of reasons I have the following situation:

## ---- file\_a.rb -------------------------------------- class A … magic loading goes here… end

## ---- file\_b.rb -------------------------------------- class B … magic loading goes here… end

## ---- magic\_file.rb ----------------------------- … does some stuff…

How can I include the contents of magic\_file.rb into both a and b “as  
is”.  
I’ve tried load() and it changes the scope so that magic\_file.rb doesn’t  
realize it’s in A or B.

I guess I’m looking for the PHP include() equivalent.

I realize this is a weird question and I realize in most situations this  
is _not_ the way to do it, but I don’t have a choice… so please no “do  
it this other way” solutions 🙂

Thanks!

-philip

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [May 8, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/load-ing-a-file-and-keeping-the-current-scope/94605/2 "2006-05-08T22:11:57Z")

</div>

On May 8, 2006, at 4:05 PM, Philip H. wrote:

> ---- file\_b.rb --------------------------------------  
> magic\_file.rb doesn’t realize it’s in A or B.

> 

class A  
eval(File.read(“magic\_file.rb”))  
end

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [May 8, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/load-ing-a-file-and-keeping-the-current-scope/94605/3 "2006-05-08T22:14:59Z")

</div>

On 5/8/06, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How can I include the contents of magic\_file.rb into both a and b “as is”.  
> I’ve tried load() and it changes the scope so that magic\_file.rb doesn’t  
> realize it’s in A or B.
> 
> I guess I’m looking for the PHP include() equivalent.
> 
> I realize this is a weird question and I realize in most situations this  
> is _not_ the way to do it, but I don’t have a choice… so please no “do  
> it this other way” solutions 🙂

I believe the following is your only choice:

eval File.read(“magic\_file.rb”)

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [May 8, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/load-ing-a-file-and-keeping-the-current-scope/94605/4 "2006-05-08T22:15:07Z")

</div>

On May 8, 2006, at 4:08 PM, Logan C. wrote:

> > end
> > 
> > Thanks!

I realize this is against your “no other solutions” rule, but why won’t

% cat magic\_file.rb  
module Stuff  
…  
end

% cat a.rb  
class A  
require ‘magic\_file’  
include Stuff  
end

work?

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [May 8, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/load-ing-a-file-and-keeping-the-current-scope/94605/5 "2006-05-08T22:18:28Z")

</div>

From: Logan C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> > * * *
> > 
> > How can I include the contents of magic\_file.rb into both a and b  
> > “as is”.  
> > …
> > 
> > -philip
> 
> class A  
> eval(File.read(“magic\_file.rb”))  
> end

Interesting solution. What about its effectivenes? And in general,  
effectivenes of require’ing something vs. eval’ing it?

Victor

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [May 8, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/load-ing-a-file-and-keeping-the-current-scope/94605/6 "2006-05-08T23:24:01Z")

</div>

> > > … magic loading goes here…  
> > > I guess I’m looking for the PHP include() equivalent.
> > 
> > > 
> > 
> > class A  
> > eval(File.read(“magic\_file.rb”))  
> > end

That worked. Thanks!

> include Stuff  
> end
> 
> work?

Not sure… (i’m fairly new to ruby) but I tried it and it doesn’t.  
Here’s specific details on what I’m doing (rails web services):

[http://www.ruby-forum.com/topic/65015#new](http://www.ruby-forum.com/topic/65015#new)

-philip

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [May 8, 2006, 11:49pm UTC](https://www.ruby-forum.com/t/load-ing-a-file-and-keeping-the-current-scope/94605/7 "2006-05-08T23:49:34Z")

</div>

On May 8, 2006, at 5:23 PM, Philip H. wrote:

> > > > * * *
> > > > 
> > > > Thanks!  
> > > > % cat magic\_file.rb  
> > > > work?
> 
> Not sure… (i’m fairly new to ruby) but I tried it and it doesn’t.  
> Here’s specific details on what I’m doing (rails web services):
> 
> [Including common code among multiple web services issue? - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/65015#new)
> 
> -philip

% cat magic\_stuff.rb  
module FindNewerThan  
def self.included(other)  
other.class\_eval do  
api\_method :find\_newer\_than,  
:expects =\> [{:created\_at =\> :datetime}],  
:returns =\> [[Class.const\_get(self.name.to\_s.gsub(/Api$/, ‘’))]]  
end  
end  
end

% cat a.rb  
require ‘magic\_stuff.rb’  
class A  
include FindNewerThan  
end

class B  
include FindNewerThan  
end

etc…

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [May 8, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/load-ing-a-file-and-keeping-the-current-scope/94605/8 "2006-05-08T22:25:10Z")

</div>

On May 8, 2006, at 4:16 PM, Victor S. wrote:

> > > class A  
> > > ---- magic\_file.rb -----------------------------  
> > > class A  
> > > eval(File.read(“magic\_file.rb”))  
> > > end
> 
> Interesting solution. What about its effectivenes? And in general,  
> effectivenes of require’ing something vs. eval’ing it?
> 
> Victor

Well require-ing only parses the file once. Other than that, this  
takes more memory (since it puts the entire file inside a string) but  
other than that I’m pretty sure the difference is effectively zero.  
The ‘ideal’ solution to this problem would be if load took an  
optional binding argument  
e.g.:

class A  
load(“magic\_file.rb”, false, binding)  
end
