Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Dynamically loading and executing within a new module?

Reply
Thread Tools

Dynamically loading and executing within a new module?

 
 
Lloyd Zusman
Guest
Posts: n/a
 
      02-19-2005
I'm not sure what would be the best way to accomplish the following in
ruby:

I'm writing a program that periodically looks for files in a certain
directory. If it finds new ones, I want it to ...

1. Dynamically create a new module whose name is based somehow upon the
name of the file.

2. Do a Kernel#load on the file within this newly created module.

3. Invoke a method called #init that is defined in this file (assuming
that this method is actually defined), and do the invocation within
the context of that newly-created module.

For example, suppose my directory contains the following files:

foo.module
----------
def init
puts "invoking #init within #{self}"
end

bar.module
----------
def init
puts "invoking #init within #{self}"
end

When my program encounters the "foo.module" file, I'd like it to do the
following:

- create a module called Foo (the name of the file with ".module"
stripped off and the first letter capitalized) ... or maybe
SomethingElse::Foo, to avoid collisions with existing packages

- do a load() of the "foo.module" file within the context of this
newly created Foo module (or SomethingElse::Foo module)

- check to see if the #init method is now defined within the
Foo module; if so, invoke that as a class method within the
context of that module (i.e., as if it were invoked like
Foo#init); this should cause the following message to
print:

invoking #init within Foo

What is the recommended way for accomplishing all this within Ruby?

--
Lloyd Zusman
http://www.velocityreviews.com/forums/(E-Mail Removed)
God bless you.



 
Reply With Quote
 
 
 
 
ES
Guest
Posts: n/a
 
      02-19-2005

On 2/19/2005, "Lloyd Zusman" <(E-Mail Removed)> wrote:

>I'm not sure what would be the best way to accomplish the following in
>ruby:
>
>I'm writing a program that periodically looks for files in a certain
>directory. If it finds new ones, I want it to ...
>
>1. Dynamically create a new module whose name is based somehow upon the
> name of the file.
>
>2. Do a Kernel#load on the file within this newly created module.
>
>3. Invoke a method called #init that is defined in this file (assuming
> that this method is actually defined), and do the invocation within
> the context of that newly-created module.
>
>For example, suppose my directory contains the following files:
>
> foo.module
> ----------
> def init
> puts "invoking #init within #{self}"
> end
>
> bar.module
> ----------
> def init
> puts "invoking #init within #{self}"
> end
>
>When my program encounters the "foo.module" file, I'd like it to do the
>following:
>
> - create a module called Foo (the name of the file with ".module"
> stripped off and the first letter capitalized) ... or maybe
> SomethingElse::Foo, to avoid collisions with existing packages
>
> - do a load() of the "foo.module" file within the context of this
> newly created Foo module (or SomethingElse::Foo module)
>
> - check to see if the #init method is now defined within the
> Foo module; if so, invoke that as a class method within the
> context of that module (i.e., as if it were invoked like
> Foo#init); this should cause the following message to
> print:
>
> invoking #init within Foo
>
>What is the recommended way for accomplishing all this within Ruby?


If you don't just want to write an actual module and then call its #init,
you could do something like this. Somewhat pseudo:

Thread.new do
if fname
mod = eval fname.capitalize
data = File.new(fname, 'r') do |f|
f.read
end
mod.module_eval data
mod.send :init, args
# ...
end
end

Or sumptin

> Lloyd Zusman


E



 
Reply With Quote
 
 
 
 
Lloyd Zusman
Guest
Posts: n/a
 
      02-19-2005
ES <(E-Mail Removed)> writes:

> On 2/19/2005, "Lloyd Zusman" <(E-Mail Removed)> wrote:
>
>> [ ... ]
>>
>>When my program encounters the "foo.module" file, I'd like it to do the
>>following:
>>
>> - create a module called Foo (the name of the file with ".module"
>> stripped off and the first letter capitalized) ... or maybe
>> SomethingElse::Foo, to avoid collisions with existing packages
>>
>> - do a load() of the "foo.module" file within the context of this
>> newly created Foo module (or SomethingElse::Foo module)
>>
>> - check to see if the #init method is now defined within the
>> Foo module; if so, invoke that as a class method within the
>> context of that module (i.e., as if it were invoked like
>> Foo#init); this should cause the following message to
>> print:
>>
>> invoking #init within Foo
>>
>>What is the recommended way for accomplishing all this within Ruby?

>
> If you don't just want to write an actual module and then call its #init,
> you could do something like this. Somewhat pseudo:
>
> Thread.new do
> if fname
> mod = eval fname.capitalize
> data = File.new(fname, 'r') do |f|
> f.read
> end
> mod.module_eval data
> mod.send :init, args
> # ...
> end
> end
>
> Or sumptin


Thanks. This makes sense.

However, the "eval fname.capitalize" part doesn't create a new module,
and that's part of what is confusing me about all this. Given a string
"Foo", what do I do to it in order to create a module object with that
name?


--
Lloyd Zusman
(E-Mail Removed)
God bless you.



 
Reply With Quote
 
Lloyd Zusman
Guest
Posts: n/a
 
      02-19-2005
Lloyd Zusman <(E-Mail Removed)> writes:

> ES <(E-Mail Removed)> writes:
>
>>
>> [ ... ]
>>
>> If you don't just want to write an actual module and then call its #init,
>> you could do something like this. Somewhat pseudo:
>>
>> Thread.new do
>> if fname
>> mod = eval fname.capitalize
>> data = File.new(fname, 'r') do |f|
>> f.read
>> end
>> mod.module_eval data
>> mod.send :init, args
>> # ...
>> end
>> end
>>
>> Or sumptin

>
> Thanks. This makes sense.
>
> However, the "eval fname.capitalize" part doesn't create a new module,
> and that's part of what is confusing me about all this. Given a string
> "Foo", what do I do to it in order to create a module object with that
> name?


Actually, I now realize that I don't need a bona fide named module, but
rather, just a way to associate a name with a module. Therefore, I
think that I can do it more or less like this:

modhash = Hash.new
if fname then
if modhash.has_key?(fname) then
mod = modhash[fname]
else
mod = Module.new
modhash[fname] = mod
end
data = File.new(fname, 'r') do |f|
f.read
end
mod.module_eval data
mod.send :init, args
end

Does this make sense?


--
Lloyd Zusman
(E-Mail Removed)
God bless you.



 
Reply With Quote
 
Christoph
Guest
Posts: n/a
 
      02-19-2005
Lloyd Zusman schrieb:

>However, the "eval fname.capitalize" part doesn't create a new module,
>and that's part of what is confusing me about all this. Given a string
>"Foo", what do I do to it in order to create a module object with that
>name?
>
>

use Module#new - for example

---
MyNameSpace.const_set fname.capitalize, Module.new
---

You can automate things

class MyModule < Module
def initialize(fname)
::MyNameSpace.const_set fname.capitalize, self
# maybe add the the MyModule method
# using the text file ....
end
end

/Christoph


 
Reply With Quote
 
Christoph
Guest
Posts: n/a
 
      02-19-2005
Lloyd Zusman schrieb:

>Actually, I now realize that I don't need a bona fide named module, but
>rather, just a way to associate a name with a module. Therefore, I
>think that I can do it more or less like this:
>
> modhash = Hash.new
> if fname then
> if modhash.has_key?(fname) then
> mod = modhash[fname]
> else
> mod = Module.new
> modhash[fname] = mod
> end
> data = File.new(fname, 'r') do |f|
> f.read
> end
> mod.module_eval data
> mod.send :init, args
> end
>
>Does this make sense?
>
>
>
>

You can also use MyNameSpace#const_get
with a rescue clause

/Christoph


 
Reply With Quote
 
ES
Guest
Posts: n/a
 
      02-20-2005

On 2/19/2005, "Lloyd Zusman" <(E-Mail Removed)> wrote:

>Lloyd Zusman <(E-Mail Removed)> writes:
>
>> ES <(E-Mail Removed)> writes:
>>
>>>
>>> [ ... ]
>>>
>>> If you don't just want to write an actual module and then call its #init,
>>> you could do something like this. Somewhat pseudo:
>>>
>>> Thread.new do
>>> if fname
>>> mod = eval fname.capitalize
>>> data = File.new(fname, 'r') do |f|
>>> f.read
>>> end
>>> mod.module_eval data
>>> mod.send :init, args
>>> # ...
>>> end
>>> end
>>>
>>> Or sumptin

>>
>> Thanks. This makes sense.
>>
>> However, the "eval fname.capitalize" part doesn't create a new module,
>> and that's part of what is confusing me about all this. Given a string
>> "Foo", what do I do to it in order to create a module object with that
>> name?

>
>Actually, I now realize that I don't need a bona fide named module, but
>rather, just a way to associate a name with a module. Therefore, I
>think that I can do it more or less like this:
>
> modhash = Hash.new
> if fname then
> if modhash.has_key?(fname) then
> mod = modhash[fname]
> else
> mod = Module.new
> modhash[fname] = mod
> end
> data = File.new(fname, 'r') do |f|
> f.read
> end
> mod.module_eval data
> mod.send :init, args
> end
>
>Does this make sense?


Yes, but I'm not sure if I understand why you're doing this in the first
place. Why don't you just create a named module, store it in a file and
load
it when requested instead of just storing the code that'll go in the
module?

> Lloyd Zusman


E



 
Reply With Quote
 
Lloyd Zusman
Guest
Posts: n/a
 
      02-20-2005
ES <(E-Mail Removed)> writes:

> On 2/19/2005, "Lloyd Zusman" <(E-Mail Removed)> wrote:
>>
>> [ ... ]
>>
>>Actually, I now realize that I don't need a bona fide named module, but
>>rather, just a way to associate a name with a module. Therefore, I
>>think that I can do it more or less like this:
>>
>> modhash = Hash.new
>> if fname then
>> if modhash.has_key?(fname) then
>> mod = modhash[fname]
>> else
>> mod = Module.new
>> modhash[fname] = mod
>> end
>> data = File.new(fname, 'r') do |f|
>> f.read
>> end
>> mod.module_eval data
>> mod.send :init, args
>> end
>>
>>Does this make sense?

>
> Yes, but I'm not sure if I understand why you're doing this in the
> first place. Why don't you just create a named module, store it in a
> file and load it when requested instead of just storing the code
> that'll go in the module?


Because I want to share code (via symbolic links) and associate it with
several modules, each module being named differently, based on the file
in which it resides. Also, I may change the names of these files from
time to time, and when I do, I don't want to also have to remember to
also change the module names contained in these files.

By dynamically naming the modules in the ways we have been discussing,
this allows me to have a unique namespace for each file-based module
that I create, even if several files share the same contents via
symbolic links. And this is ensured no matter how many renames
or symbolic link changes I make.

--
Lloyd Zusman
(E-Mail Removed)
God bless you.



 
Reply With Quote
 
Lloyd Zusman
Guest
Posts: n/a
 
      02-20-2005
Christoph <(E-Mail Removed)> writes:

> Lloyd Zusman schrieb:
>
>>Actually, I now realize that I don't need a bona fide named module, but
>>rather, just a way to associate a name with a module. Therefore, I
>>think that I can do it more or less like this:
>>
>> modhash = Hash.new
>> if fname then
>> if modhash.has_key?(fname) then
>> mod = modhash[fname]
>> else
>> mod = Module.new
>> modhash[fname] = mod
>> end
>> data = File.new(fname, 'r') do |f|
>> f.read
>> end
>> mod.module_eval data
>> mod.send :init, args
>> end
>>
>>Does this make sense?
>>
>>
>>
>>

> You can also use MyNameSpace#const_get
> with a rescue clause


Yes ... makes sense. Thanks.

--
Lloyd Zusman
(E-Mail Removed)
God bless you.



 
Reply With Quote
 
Lloyd Zusman
Guest
Posts: n/a
 
      02-20-2005
Christoph <(E-Mail Removed)> writes:

> Lloyd Zusman schrieb:
>
>>However, the "eval fname.capitalize" part doesn't create a new module,
>>and that's part of what is confusing me about all this. Given a string
>>"Foo", what do I do to it in order to create a module object with that
>>name?
>>
>>

> use Module#new - for example
>
> ---
> MyNameSpace.const_set fname.capitalize, Module.new
> ---
>
> You can automate things
>
> class MyModule < Module
> def initialize(fname)
> ::MyNameSpace.const_set fname.capitalize, self
> # maybe add the the MyModule method
> # using the text file ....
> end
> end
> /Christoph


Aha! ... that's a great idea. I love how ruby (like Smalltalk) makes
classes, objects, etc. into first-class objects.

--
Lloyd Zusman
(E-Mail Removed)
God bless you.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including and Executing JS and CSS from an ASPX within a Master Pa Alex Maghen ASP .Net 3 08-12-2010 12:59 AM
Building and executing SQL Query dynamically, best practices =?Utf-8?B?V2ViTWF0cml4?= ASP .Net 6 05-15-2008 03:15 PM
Dynamical loading of html files and executing of its javascript content. mowsen@googlemail.com Javascript 9 06-03-2007 07:34 AM
Executing a validating query before loading a page Patrick Olurotimi Ige ASP .Net 2 01-27-2005 06:00 PM
Loading and executing an arbitrary Python script from within Python TheDustbustr Python 2 08-17-2003 02:42 AM



Advertisments