Dynamic include

I’m developing a program which needs to dynamically load
and unload modules, adding the methods of each module to
a class. ``include’’ is giving me trouble.

The following seems logical to me, but “include” isn’t
known inside the method:

class Quinn
def quinn_loadModule(modname)
load "modules/#{modname}.rb"
include #{modname}
end
end

The following also doesn’t work as I expected it to.
Although it executes without error, the methods in
the module are not extended into the class:

class Quinn < Module
def quinn_loadModule(modname)
load "modules/#{modname}.rb"
include #{modname}
end
end

The following works, but it feels dirty:

class Quinn
def quinn_loadModule(modname)
require “ftools”

  tmp = File::new("module.tmp", "w+")
  tmp.puts "load \"modules/#{modname}.rb\""
  tmp.puts "include #{modname}"
  tmp.close

  load "module.tmp"
  File::safe_unlink("module.tmp")

end
end

I’m aware of extend_object(), but requiring modules to
add themselves to a particular object, IMO, takes away
the modularity from a module.

Any ideas are welcome.

···


timothy(at)linuxfromscratch.org

-- “Share and Enjoy” || “Go stick your head in a pig” --

In article 20030223231845.GA7488@quasar,

I’m developing a program which needs to dynamically load
and unload modules, adding the methods of each module to
a class. ``include’’ is giving me trouble.

The following seems logical to me, but “include” isn’t
known inside the method:

class Quinn
def quinn_loadModule(modname)
load “modules/#{modname}.rb”
include #{modname}
end
end

The following also doesn’t work as I expected it to.
Although it executes without error, the methods in
the module are not extended into the class:

class Quinn < Module
def quinn_loadModule(modname)
load “modules/#{modname}.rb”
include #{modname}
end
end

include is a method on class Module and I believe that Class already
inherits from Module - I don’t think you want Quinn to inherit from
Module. To get this to work you’d have to make ‘quinn_loadModule’ a
class method, like:

class Quinn
def Quinn.loadModule(modname)
load “modules/#{modname}.rb”
include #{modname}
end
end

…I think that should do what you want.

Phil

···

Timothy Bauscher timothy@linuxfromscratch.org wrote:

Hi –

···

On Mon, 24 Feb 2003, Phil Tomson wrote:

include is a method on class Module and I believe that Class already
inherits from Module - I don’t think you want Quinn to inherit from
Module. To get this to work you’d have to make ‘quinn_loadModule’ a
class method, like:

class Quinn
def Quinn.loadModule(modname)
load “modules/#{modname}.rb”
include #{modname}
end
end

The include #{modname} thing will be read as “include” plus a comment,
though :slight_smile:

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

include is a method on class Module and I believe that Class already
inherits from Module - I don’t think you want Quinn to inherit from
Module. To get this to work you’d have to make ‘quinn_loadModule’ a
class method, like:

class Quinn
def Quinn.loadModule(modname)
load “modules/#{modname}.rb”
include #{modname}
end
end

Thanks Phil, that worked.

The include #{modname} thing will be read as “include” plus a comment,
though :slight_smile:

And an eval() statement was necessary as well.

Thanks.

···

On Mon, Feb 24, 2003 at 11:13:15AM +0900, dblack@candle.superlink.net wrote:


timothy(at)linuxfromscratch.org

-- “Share and Enjoy” || “Go stick your head in a pig” --

Hi –

include is a method on class Module and I believe that Class already
inherits from Module - I don’t think you want Quinn to inherit from
Module. To get this to work you’d have to make ‘quinn_loadModule’ a
class method, like:

class Quinn
def Quinn.loadModule(modname)
load “modules/#{modname}.rb”
include #{modname}
end
end

Thanks Phil, that worked.

The include #{modname} thing will be read as “include” plus a comment,
though :slight_smile:

And an eval() statement was necessary as well.

You should be able to dispense with that; try this:

include Object.const_get(modname.to_s)

and you could then do:

Quinn.load_module(Modname) # constant

or

Quinn.load_module(“Modname”) # string

David

···

On Mon, 24 Feb 2003, Timothy Bauscher wrote:

On Mon, Feb 24, 2003 at 11:13:15AM +0900, dblack@candle.superlink.net wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav