Best way to do dynamic mixin or dynamic include? (Mixin modu

I am searching for the best way to do a dynamic mixin with Ruby.

Basically I have a class that includes a mixin but I want the mixin
module
name to be driven from configuration and not the code.

class A
include Foo
end

but instead of Foo being constant module name, I would this to be driven
off
of a configuration value. (I’m not too worried about the module being
loaded
previously that is easily taken care of, so primarily just wanting to
know
the best way to do a dynamic include).

Any opinions on the best way to accomplish this?

A very simple way to approach this might be something like

ModToInclude = ‘Foo’

class A
eval( “include #{ModToInclude}” )
end

Any better ideas?

Thanks in advance,

Jeff

On Wed, 7 Feb 2007, Jeff B. wrote:

but instead of Foo being constant module name, I would this to be driven off
of a configuration value. (I’m not too worried about the module being loaded
previously that is easily taken care of, so primarily just wanting to know
the best way to do a dynamic include).

Any opinions on the best way to accomplish this?

A very simple way to approach this might be something like

ModToInclude = ‘Foo’

ModToInclude = ‘Foo; system “sudo rm -rf /”’

class A
eval( “include #{ModToInclude}” )
end

Any better ideas?

def factory plugin
plugins = {
:foo => Foo,
:bar => Bar,
}
Class.new{
include plugins[ plugin ]
}
end

A = factory :foo

is one simple way.

regards.

-a

On Feb 6, 2:29 pm, “Jeff B.” [email protected]
wrote:

I am searching for the best way to do a dynamic mixin with Ruby.

Basically I have a class that includes a mixin but I want the mixin module
name to be driven from configuration and not the code.

class A
include Foo
end

Ara’s is probably best, but FYI

include const_get(module_name)

T.

On Wed, 7 Feb 2007 [email protected] wrote:

On Feb 6, 2007, at 2:56 PM, [email protected] wrote:

ModToInclude = ‘Foo; system “sudo rm -rf /”’

Ugh. I get shivers just seeing this sort of thing in ‘print’. It is
a common example of what not to do but I’ll bet someone, somewhere,
has said ‘Gee, I wonder what this does?’. After a quick click-drag,
copy, paste; someone, somewhere, is having a bad day.

lol! that’s terrible! never thought of that.

Perhaps posts like this should have a mandatory disclosure:

The code snippets you see here were constructed by trained
professionals. Do NOT try these snippets at home.

indeed!

-a

Thanks Ara and Trans for your quick responses and solutions. Depending
on
the circumstances I can see uses for both solutions.

Have a great day!!

Jeff

On Feb 6, 2007, at 2:56 PM, [email protected] wrote:

ModToInclude = ‘Foo; system “sudo rm -rf /”’

Ugh. I get shivers just seeing this sort of thing in ‘print’. It is
a common example of what not to do but I’ll bet someone, somewhere,
has said ‘Gee, I wonder what this does?’. After a quick click-drag,
copy, paste; someone, somewhere, is having a bad day.

Perhaps posts like this should have a mandatory disclosure:

The code snippets you see here were constructed by trained
professionals. Do NOT try these snippets at home.

:slight_smile:

Gary W.

On Feb 6, 1:16 pm, [email protected] wrote:

Ugh. I get shivers just seeing this sort of thing in ‘print’. It is
a common example of what not to do but I’ll bet someone, somewhere,
has said ‘Gee, I wonder what this does?’. After a quick click-drag,
copy, paste; someone, somewhere, is having a bad day.

The good news is that it’s probable that you won’t hear from that
person for…well, probably at least a couple of days before the
system is up and working again. :slight_smile:

On Wed, 7 Feb 2007, Phrogz wrote:

On Feb 6, 1:16 pm, [email protected] wrote:

Ugh. I get shivers just seeing this sort of thing in ‘print’. It is
a common example of what not to do but I’ll bet someone, somewhere,
has said ‘Gee, I wonder what this does?’. After a quick click-drag,
copy, paste; someone, somewhere, is having a bad day.

The good news is that it’s probable that you won’t hear from that
person for…well, probably at least a couple of days before the
system is up and working again. :slight_smile:

that reminds me of a presentation on drb i did once: we set up a mini
cluster
with everyones computer feeding from a tuplespace. then i submitted the
job

ruby -e’ require “tmpdir”; loop{ fork{ open(File.join(Dir.tmpdir,
$$.to_s),“w”){|f| f.write(0.chr * (2**20) } } } ’

it’s hard to reboot when /tmp is full!

:wink:

-a

On 2/6/07, [email protected] [email protected] wrote:

system is up and working again. :slight_smile:

that reminds me of a presentation on drb i did once: we set up a mini cluster
with everyones computer feeding from a tuplespace. then i submitted the job

ruby -e’ require “tmpdir”; loop{ fork{ open(File.join(Dir.tmpdir, $$.to_s),“w”){|f| f.write(0.chr * (2**20) } } } ’

it’s hard to reboot when /tmp is full!

:wink:

Seeing that code is about the only thing today that makes me happy I’m
developing on a Windows box – no fork for me :slight_smile:

TwP

On Feb 6, 2007, at 5:16 PM, [email protected] wrote:

that reminds me of a presentation on drb i did once: we set up a
mini cluster
with everyones computer feeding from a tuplespace. then i
submitted the job

ruby -e’ require “tmpdir”; loop{ fork{ open(File.join
(Dir.tmpdir, $$.to_s),“w”){|f| f.write(0.chr * (2**20) } } } ’

Oh geez. Stand back. There he goes again, waving around those sharp
pointy
snippets of code. Ara, is your keyboard registered as a deadly weapon?

:slight_smile:

Gary W.