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”
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
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
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
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
And an eval() statement was necessary as well.
You should be able to dispense with that; try this: