[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

temporary require ,is it possible?

gz zz

5/24/2007 9:03:00 AM

I have a idea,it is "temporary require"
[code]
module Find
# my define
end

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

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

--
Posted via http://www.ruby-....

7 Answers

Brian Candler

5/24/2007 9:53:00 AM

0

On Thu, May 24, 2007 at 06:03:22PM +0900, Gpy Good wrote:
> I have a idea,it is "temporary require"
> [code]
> module Find
> # my define
> end
>
> tmp_require("find"){
> #do something with "Find.find" method,temporary!!
> }
>
> 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 }

?

barjunk

5/24/2007 6:47:00 PM

0

On May 24, 1:52 am, Brian Candler <B.Cand...@pobox.com> 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
<snip>

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.

Joel VanderWerf

5/24/2007 7:39:00 PM

0

Brian Candler 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
>> # my define
>> end
>>
>> tmp_require("find"){
>> #do something with "Find.find" method,temporary!!
>> }
>>
>> 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.n...

No magic, just module_eval.

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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Axel Etzold

5/24/2007 8:17:00 PM

0

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

http://raa.ruby-lang.org/gonzui/markup/unrequireable/unrequ...

Best regards,

Axel
--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: http://www.gmx.net/de/...

Rick DeNatale

5/24/2007 8:22:00 PM

0

On 5/24/07, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> Brian Candler 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
> >> # my define
> >> end
> >>
> >> tmp_require("find"){
> >> #do something with "Find.find" method,temporary!!
> >> }
> >>
> >> 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.n...
>
> 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.ne...
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

ara.t.howard

5/24/2007 8:44:00 PM

0


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

> I have a idea,it is "temporary require"
> [code]
> module Find
> # my define
> end
>
> tmp_require("find"){
> #do something with "Find.find" method,temporary!!
> }
>
> puts Find # my define,no "Find.find" method
> [/code]
> Is it possible?
>
> --
> Posted via http://www.ruby-....
>

why not simply fork and do something?

-a
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




gz zz

5/25/2007 3:01:00 AM

0

Axel Etzold wrote:
> Maybe this can help to address the OP's question:
>
> http://raa.ruby-lang.org/gonzui/markup/unrequireable/unrequ...
>
> Best regards,
>
> Axel
Thanks,It seems that my need.
And,but...
[code]
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
[/code]
It's my goal.^_^

--
Posted via http://www.ruby-....