T
Travis Whitton
Hi all,
I'm having some trouble with trying to dynamically load a module at runtime.
The following example demonstrates the problem I'm encountering.
dyninclude.rb
--------------------
class DynamicInclude
def initialize(dynmod)
DynamicInclude.load_module(dynmod)
end
def DynamicInclude.load_module(modname)
load "#{modname}.rb"
include Object.const_get(modname.to_s)
end
end
Hello.rb
--------------------
module Hello
def greeting
puts "Hello"
end
end
Goodbye.rb
--------------------
module Goodbye
def greeting
puts "Goodbye"
end
end
Example
--------------------
d = DynamicInclude.new("Hello")
d.greeting() # prints Hello
e = DynamicInclude.new("Goodbye")
e.greeting() # prints Goodbye
d.greeting() # prints Goodbye
So, how can I dynamically include multiple module without the negative side
effect of previous class instances inheriting the new methods? I'm suspecting
that the fact that load_module is a class method is playing a role here, but
I can't make it work without making load_module a class method. Any help would
be appreciated.
Sincerely,
Travis Whitton <[email protected]>
I'm having some trouble with trying to dynamically load a module at runtime.
The following example demonstrates the problem I'm encountering.
dyninclude.rb
--------------------
class DynamicInclude
def initialize(dynmod)
DynamicInclude.load_module(dynmod)
end
def DynamicInclude.load_module(modname)
load "#{modname}.rb"
include Object.const_get(modname.to_s)
end
end
Hello.rb
--------------------
module Hello
def greeting
puts "Hello"
end
end
Goodbye.rb
--------------------
module Goodbye
def greeting
puts "Goodbye"
end
end
Example
--------------------
d = DynamicInclude.new("Hello")
d.greeting() # prints Hello
e = DynamicInclude.new("Goodbye")
e.greeting() # prints Goodbye
d.greeting() # prints Goodbye
So, how can I dynamically include multiple module without the negative side
effect of previous class instances inheriting the new methods? I'm suspecting
that the fact that load_module is a class method is playing a role here, but
I can't make it work without making load_module a class method. Any help would
be appreciated.
Sincerely,
Travis Whitton <[email protected]>