Is mixin possible with dynamic require?

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?

Thanks,
~Jon Hurst

Hi,

···

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


Nobu Nakada

Hi –

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” :slight_smile:

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.

David

···

On Wed, 5 Feb 2003, Jon Hurst wrote:


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

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” :slight_smile:

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.

~Jon Hurst

Wow, that is (as they say) sweet!
I never thought about doing that.

···

On Wednesday, 5 February 2003 at 10:37:40 +0900, dblack@candle.superlink.net wrote:

Hi –

On Wed, 5 Feb 2003, Jon Hurst wrote:

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.


Jim Freeze