Problem Dynamically Loading a Module

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Problem Dynamically Loading a Module

    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 <whitton@atlantic.net>
    Travis Whitton Guest

  2. Similar Questions and Discussions

    1. Changing module dynamically
      Thomas M. Widmann sikyal: I'm not sure how much control you have over the architecture of the whole thing, but this is how I would implement...
    2. #24199 [Fbk->Csd]: problem with loading module in apache 2.0.46
      ID: 24199 Updated by: sniper@php.net Reported By: keeper at fly dot srk dot fer dot hr -Status: Feedback...
    3. #24199 [Fbk]: problem with loading module in apache 2.0.46
      ID: 24199 Updated by: sniper@php.net Reported By: keeper at fly dot srk dot fer dot hr Status: Feedback Bug...
    4. #24199 [Opn->Fbk]: problem with loading module in apache 2.0.46
      ID: 24199 Updated by: sniper@php.net Reported By: keeper at fly dot srk dot fer dot hr -Status: Open +Status: ...
    5. #24199 [Opn]: problem with loading module in apache 2.0.46
      ID: 24199 User updated by: keeper at fly dot srk dot fer dot hr Reported By: keeper at fly dot srk dot fer dot hr Status: ...
  3. #2

    Default Re: Problem Dynamically Loading a Module

    >>>>> "T" == Travis Whitton <whitton@atlantic.net> 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 Guest

  4. #3

    Default Re: Problem Dynamically Loading a Module

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

    Travis

    In article <200309151635.h8FGZgT05555@moulon.inra.fr>, ts wrote:
    >>>>>> "T" == Travis Whitton <whitton@atlantic.net> 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 Guest

  5. #4

    Default Problem Dynamically Loading a Module

    Thanks a lot for the load and extend usage. I was trying with include which failed of course. This was great help.
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139