Problem Dynamically Loading a Module

Discussion in 'Ruby' started by Travis Whitton, Sep 15, 2003.

  1. 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 <>
    Travis Whitton, Sep 15, 2003
    #1
    1. Advertising

  2. Travis Whitton

    ts Guest

    >>>>> "T" == Travis Whitton <> writes:


    Try this

    T> class DynamicInclude
    T> def initialize(dynmod)
    T> DynamicInclude.load_module(dynmod)

    extend(Object.const_get(dynmod.to_s))

    T> end

    T> def DynamicInclude.load_module(modname)
    T> load "#{modname}.rb"
    T> include Object.const_get(modname.to_s)

    you don't need this include

    T> end
    T> end

    Now if you are sure that these previous module don't exist you can write

    svg% cat b.rb
    #!/usr/bin/ruby

    class DynamicInclude
    def initialize(dynmod)
    DynamicInclude.load_module(dynmod)
    extend(Object.instance_eval { remove_const(dynmod.to_s) })
    end

    def DynamicInclude.load_module(modname)
    load "#{modname}.rb"
    end
    end

    d = DynamicInclude.new("Hello")
    d.greeting() # prints Hello
    e = DynamicInclude.new("Goodbye")
    e.greeting() # prints Goodbye
    d.greeting() # prints Goodbye
    svg%

    svg% b.rb
    Hello
    Goodbye
    Hello
    svg%

    The load is made in Object, then the constant is removed to don't pollute
    Object


    Guy Decoux
    ts, Sep 15, 2003
    #2
    1. Advertising

  3. Guy,
    Thanks very much! That solved my problem perfectly. I love the Ruby community.

    Travis

    In article <>, ts wrote:
    >>>>>> "T" == Travis Whitton <> writes:

    >
    >
    > Try this
    >
    >T> class DynamicInclude
    >T> def initialize(dynmod)
    >T> DynamicInclude.load_module(dynmod)
    >
    > extend(Object.const_get(dynmod.to_s))
    >
    >T> end
    >
    >T> def DynamicInclude.load_module(modname)
    >T> load "#{modname}.rb"
    >T> include Object.const_get(modname.to_s)
    >
    > you don't need this include
    >
    >T> end
    >T> end
    >
    > Now if you are sure that these previous module don't exist you can write
    >
    > svg% cat b.rb
    > #!/usr/bin/ruby
    >
    > class DynamicInclude
    > def initialize(dynmod)
    > DynamicInclude.load_module(dynmod)
    > extend(Object.instance_eval { remove_const(dynmod.to_s) })
    > end
    >
    > def DynamicInclude.load_module(modname)
    > load "#{modname}.rb"
    > end
    > end
    >
    > d = DynamicInclude.new("Hello")
    > d.greeting() # prints Hello
    > e = DynamicInclude.new("Goodbye")
    > e.greeting() # prints Goodbye
    > d.greeting() # prints Goodbye
    > svg%
    >
    > svg% b.rb
    > Hello
    > Goodbye
    > Hello
    > svg%
    >
    > The load is made in Object, then the constant is removed to don't pollute
    > Object
    >
    >
    > Guy Decoux
    >
    Travis Whitton, Sep 15, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maric Michaud
    Replies:
    0
    Views:
    7,181
    Maric Michaud
    Jun 24, 2006
  2. hg
    Replies:
    7
    Views:
    283
    laurent rahuel
    Jan 5, 2007
  3. Rafe
    Replies:
    3
    Views:
    275
    Jeremy Sanders
    Dec 1, 2008
  4. Lloyd Zusman
    Replies:
    9
    Views:
    151
    Lloyd Zusman
    Feb 20, 2005
  5. Vito Corleone

    Loading module in another module

    Vito Corleone, Jan 10, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    117
    Shawn Corey
    Jan 10, 2005
Loading...

Share This Page