I need to dynamically require a file(module) from within a class. I then
need to mixin a method from this module. However, include doesn’t seem to be
supported from within a method. I cannot place include outside of a method,
because the module has not yet been required. How can this be solved?
At Wed, 5 Feb 2003 10:05:25 +0900, Jon Hurst wrote:
I need to dynamically require a file(module) from within a class. I then
need to mixin a method from this module. However, include doesn’t seem to be
supported from within a method. I cannot place include outside of a method,
because the module has not yet been required. How can this be solved?
class Foo
def mixin(mod)
self.class.class_eval {include mod}
end
end
I need to dynamically require a file(module) from within a class. I then
need to mixin a method from this module. However, include doesn’t seem to be
supported from within a method. I cannot place include outside of a method,
because the module has not yet been required. How can this be solved?
I’m not sure exactly what you’re trying to do. When you say dynamically
require a file from within a class, do you mean something like this:
class MyClass
require ‘something’
end
When you say “mixin a method from this module”, I think you mean “mixin
a module from this file”
Finally, is it possible you really want to extend the current object,
rather than include it in the object’s class? Something like:
class MyClass
require ‘something’ # contains SomeModule
def my_method
extend(SomeModule)
end
end
This would add the methods in SomeModule to the current object.
I’m not sure exactly what you’re trying to do. When you say dynamically
require a file from within a class, do you mean something like this:
class MyClass
require ‘something’
end
When you say “mixin a method from this module”, I think you mean “mixin
a module from this file”
You correctly parsed my vague question on both counts,
Finally, is it possible you really want to extend the current object,
rather than include it in the object’s class?
and you astutely determined that I don’t really want to do what I stated I
want to do. You deserve some sort of interpretation prize! I am new to Ruby
and overlooked this (important) detail, so thanks for more help than I knew
I needed.