Forum: Ruby temporary require ,is it possible?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ba608f20b6707017d9812b9b3f1406e3?d=identicon&s=25 gpy good (gpygood)
on 2007-05-24 11:03
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?
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-05-24 11:54
(Received via mailing list)
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 }

?
2f5c0f4501526070f1df8a01b210b6b5?d=identicon&s=25 barjunk (Guest)
on 2007-05-24 20:51
(Received via mailing list)
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.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-05-24 21:40
(Received via mailing list)
Brian Candler 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/

No magic, just module_eval.

(I'm not sure it solves the OP's problem, though.)
D0338c0de4cb3c5c17300396159933d1?d=identicon&s=25 Axel Etzold (Guest)
on 2007-05-24 22:18
(Received via mailing list)
Maybe this can help to address the OP's question:

http://raa.ruby-lang.org/gonzui/markup/unrequireab...

Best regards,

Axel
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-05-24 22:22
(Received via mailing list)
On 5/24/07, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> >> }
> > 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/
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 ara.t.howard (Guest)
on 2007-05-24 22:45
(Received via mailing list)
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/.
>

why not simply fork and do something?

-a
Ba608f20b6707017d9812b9b3f1406e3?d=identicon&s=25 gpy good (gpygood)
on 2007-05-25 05:00
Axel Etzold wrote:
> Maybe this can help to address the OP's question:
>
> http://raa.ruby-lang.org/gonzui/markup/unrequireab...
>
> 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.^_^
This topic is locked and can not be replied to.