R
R. Kumar
I was hoping to dynamically load and include code in my application.
After some struggle, i've got this working. Wish to know if this is the
cleanest way to do so:
Since include has a problem (NoMethod) i tried making the method Class
level,
it works but include does not take a String, so eval it
Can this not be at instance level ??
def self.load_module requirename, includename
require "xxx/#{requirename}"
eval ( "include #{includename}")
end
I wish to call an initializer method in such a module which
will bind keys (for example)
needs to be done at object level
def init_module requirename, includename
send("#{requirename}_init") #if respond_to? "#{includename}_init"
end
I was hoping that the require and include could be at the instance
level, so other objects are not affected. Different objects (instances)
may include different modules.
I tried by NOT putting an include, just trying to call the methods using
send. I also appended the module name in the send, but got a no method
error.
The only other option i thought of was to extend my class and add this
functionlity but that obviously means too many extensions, I'd like
multiple modules to be loadable this way.
Thanks.
After some struggle, i've got this working. Wish to know if this is the
cleanest way to do so:
Since include has a problem (NoMethod) i tried making the method Class
level,
it works but include does not take a String, so eval it
Can this not be at instance level ??
def self.load_module requirename, includename
require "xxx/#{requirename}"
eval ( "include #{includename}")
end
I wish to call an initializer method in such a module which
will bind keys (for example)
needs to be done at object level
def init_module requirename, includename
send("#{requirename}_init") #if respond_to? "#{includename}_init"
end
I was hoping that the require and include could be at the instance
level, so other objects are not affected. Different objects (instances)
may include different modules.
I tried by NOT putting an include, just trying to call the methods using
send. I also appended the module name in the send, but got a no method
error.
The only other option i thought of was to extend my class and add this
functionlity but that obviously means too many extensions, I'd like
multiple modules to be loadable this way.
Thanks.