temporary require ,is it possible?

G

Gpy Good

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
Is it possible?
 
B

Brian Candler

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
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 }

?
 
B

barjunk

I have a idea,it is "temporary require"
Code:
module Find[/QUOTE][/QUOTE]
<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.
 
J

Joel VanderWerf

Brian said:
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
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/

No magic, just module_eval.

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

Rick DeNatale

Brian said:
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
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/

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/
 
A

ara.t.howard

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
Is it possible?

why not simply fork and do something?

-a
 
G

gz zz

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

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

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
It's my goal.^_^
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top