# Temporary require ,is it possible?

**URL:** <https://www.ruby-forum.com/t/temporary-require-is-it-possible/104960>\
**Category:** Ruby\
**Created:** [May 24, 2007, 11:03am UTC](https://www.ruby-forum.com/t/temporary-require-is-it-possible/104960 "2007-05-24T11:03:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [May 24, 2007, 11:03am UTC](https://www.ruby-forum.com/t/temporary-require-is-it-possible/104960/1 "2007-05-24T11:03:21Z")

</div>

I have a idea,it is “temporary require”

```auto
module Find
 # my define
end

tmp_require("find"){
 #do something with "Find.find" method,temporary!!
}

puts Find # my define,no "Find.find" method
```

Is it possible?

---

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [May 24, 2007, 11:54am UTC](https://www.ruby-forum.com/t/temporary-require-is-it-possible/104960/2 "2007-05-24T11:54:00Z")

</div>

On Thu, May 24, 2007 at 06:03:22PM +0900, Gpy Good wrote:

> puts Find # my define,no “Find.find” method  
> [/code]  
> Is it possible?

Hmm, I was thinking of

temp = load(“find.rb”, true)  
temp::Find.find(…)

but unfortunately load returns true, not the module object ☹

Here’s a messy way:

require ‘find’ # the _old_ implementation of find

#require ‘new\_find’ # contains the following:  
module NewStuff  
module Find  
def self.find  
puts “hello”  
end  
end  
end

# Main program

old\_find = Find rescue nil  
begin  
Find = NewStuff::Find  
Find.find # this is the code to be executed  
ensure  
Find = old\_find  
end

Find.find(".") { |f| puts f }

?

---

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [May 24, 2007, 8:51pm UTC](https://www.ruby-forum.com/t/temporary-require-is-it-possible/104960/3 "2007-05-24T20:51:00Z")

</div>

On May 24, 1:52 am, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Thu, May 24, 2007 at 06:03:22PM +0900, Gpy Good wrote:
> 
> > I have a idea,it is “temporary require”
> > 
> > [code]  
> > module Find

Maybe I’m missing the OP’s goal, but wouldn’t you just use a module  
reference directly to get the same result? What’s the benefit of  
having a “temporary” require?

Mike B.

---

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [May 24, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/temporary-require-is-it-possible/104960/4 "2007-05-24T21:40:15Z")

</div>

Brian C. wrote:

> > puts Find # my define,no “Find.find” method  
> > [/code]  
> > Is it possible?
> 
> Hmm, I was thinking of
> 
> temp = load(“find.rb”, true)  
> temp::Find.find(…)
> 
> but unfortunately load returns true, not the module object ☹

There’s a way around that. See

[http://redshift.sourceforge.net/script/](http://redshift.sourceforge.net/script/)

No magic, just module\_eval.

(I’m not sure it solves the OP’s problem, though.)

---

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [May 24, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/temporary-require-is-it-possible/104960/5 "2007-05-24T22:18:18Z")

</div>

Maybe this can help to address the OP’s question:

[http://raa.ruby-lang.org/gonzui/markup/unrequireable/unrequireable.rb](http://raa.ruby-lang.org/gonzui/markup/unrequireable/unrequireable.rb)

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [May 24, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/temporary-require-is-it-possible/104960/6 "2007-05-24T22:45:22Z")

</div>

On May 24, 2007, at 3:03 AM, Gpy Good wrote:

> puts Find # my define,no “Find.find” method  
> [/code]  
> Is it possible?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

why not simply fork and do something?

-a

---

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [May 25, 2007, 5:00am UTC](https://www.ruby-forum.com/t/temporary-require-is-it-possible/104960/7 "2007-05-25T05:00:46Z")

</div>

Axel E. wrote:

> Maybe this can help to address the OP’s question:
> 
> [http://raa.ruby-lang.org/gonzui/markup/unrequireable/unrequireable.rb](http://raa.ruby-lang.org/gonzui/markup/unrequireable/unrequireable.rb)
> 
> Best regards,
> 
> Axel  
> Thanks,It seems that my need.  
> And,but…

```auto
def tmp_require(name)
  require 'unrequireable'
  Object::unrequireable!

  require name
  yield
  unrequire name

end

module Find
 def self.info
   "my define"
 end
end

tmp_require("find"){
 Find.find("."){}#should work,need it temporary
}

puts Find.info #my define
Find.find("."){} #should not work(because I dont define it,and not need 
it now ),but it worked
```

It’s my goal.🙂

---

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [May 24, 2007, 10:22pm UTC](https://www.ruby-forum.com/t/temporary-require-is-it-possible/104960/8 "2007-05-24T22:22:18Z")

</div>

On 5/24/07, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > }  
> > > but unfortunately load returns true, not the module object ☹
> 
> There’s a way around that. See
> 
> [Script](http://redshift.sourceforge.net/script/)
> 
> No magic, just module\_eval.
> 
> (I’m not sure it solves the OP’s problem, though.)

## Somehow this smells to me like a job for Why’s sandbox. [http://code.whytheluckystiff.net/sandbox/](http://code.whytheluckystiff.net/sandbox/)

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
