Best way to do dynamic mixin or dynamic include? (Mixin module name is defined and included at runtime)

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

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'

   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 Wed, 7 Feb 2007, Jeff Barczewski wrote:
--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama

Ara's is probably best, but FYI

  include const_get(module_name)

T.

···

On Feb 6, 2:29 pm, "Jeff Barczewski" <jeff.barczew...@gmail.com> 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

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 Wright

···

On Feb 6, 2007, at 2:56 PM, ara.t.howard@noaa.gov wrote:

ModToInclude = 'Foo; system "sudo rm -rf /"'

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 2/6/07, Trans <transfire@gmail.com> wrote:

On Feb 6, 2:29 pm, "Jeff Barczewski" <jeff.barczew...@gmail.com> > 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.

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

···

On Wed, 7 Feb 2007 gwtmp01@mac.com wrote:

On Feb 6, 2007, at 2:56 PM, ara.t.howard@noaa.gov wrote:

--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama

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 Feb 6, 1:16 pm, gwtm...@mac.com 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.

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 Wed, 7 Feb 2007, Phrogz wrote:

On Feb 6, 1:16 pm, gwtm...@mac.com 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:

--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama

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 Wright

···

On Feb 6, 2007, at 5:16 PM, ara.t.howard@noaa.gov 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) } } } '

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 2/6/07, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Wed, 7 Feb 2007, Phrogz wrote:

> On Feb 6, 1:16 pm, gwtm...@mac.com 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: