choosing a module name at runtime

Discussion in 'Ruby' started by John Smirnoff, Sep 12, 2009.

  1. My application chooses at runtime the input format of some data, for
    example :xml or :mysql. Then, the app instantiates a DataManager. The
    DataManager class should 'include' the appropriate Module (either
    DataIOXML or DataIOMySQL).
    I tried to use include in the initialize method of DataManager, but that
    doesn't work.
    Can I include a module in the initialize method of the class? If yes,
    how?
    If no: Any other ways how I can choose the correct module to include at
    runtime?

    Important: All the information about Modules and file names is stored in
    the DataManager class. My app only knows the DataManger class itself,
    not the names of the modules (so I can't subclass separate IO classes
    from DataManager, and instantiate them directly from my app).

    Here's the example code for the app, the DataManger class, and the :xml
    Module:



    #! /usr/bin/ruby -w
    # main app
    #
    # here, only the name 'DataManager' and the IO format :)xml or :mysql)
    is known,
    # not the name of the format-specific module.
    require "DataManager.rb"
    dm = DataManager.new:)xml)
    dm.say_something



    # DataManager.rb
    class DataManager

    # theDataManager class knows all possible IO formats, and the
    # corresponding Module names:
    @@io_mixins = {
    :xml => 'DataIOXML',
    :mysql => 'DataIOMySQL',
    }

    def initialize(format=:xml)

    unless @@io_mixins.include? format
    raise NotImplementedError, "IO Format #{format} unknown"
    end

    require @@io_mixins[format]+".rb"
    # the next line causes an error
    include DataIOXML

    end
    end



    # DataIOXML.rb
    # XML-specific module
    module DataIOXML
    def say_something
    puts "I'm DataIOXML"
    end
    end
    --
    Posted via http://www.ruby-forum.com/.
    John Smirnoff, Sep 12, 2009
    #1
    1. Advertising

  2. On Sep 12, 2009, at 3:26 PM, John Smirnoff wrote:

    > My application chooses at runtime the input format of some data, for
    > example :xml or :mysql. Then, the app instantiates a DataManager. The
    > DataManager class should 'include' the appropriate Module (either
    > DataIOXML or DataIOMySQL).
    > I tried to use include in the initialize method of DataManager, but
    > that
    > doesn't work.
    > Can I include a module in the initialize method of the class? If yes,
    > how?
    > If no: Any other ways how I can choose the correct module to include
    > at
    > runtime?


    You want the "extend" method, not the "include" method:

    module A
    def foo
    "A: foo"
    end
    end

    module B
    def foo
    "B: foo"
    end
    end

    class C
    def initialize which
    extend(which == :a ? A : B)
    end

    def metaclass; class << self; self end end
    end

    p C.new:)a).metaclass.ancestors
    p C.new:)b).metaclass.ancestors

    Hope that helps.

    ---
    Aaron Patterson
    http://tenderlovemaking.com
    Aaron Patterson, Sep 12, 2009
    #2
    1. Advertising

  3. Aaron Patterson wrote:

    > You want the "extend" method, not the "include" method:


    Thanks!
    --
    Posted via http://www.ruby-forum.com/.
    John Smirnoff, Sep 13, 2009
    #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. Randall Parker

    On choosing ASPX authentication cookie name

    Randall Parker, Dec 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    431
    Ken Cox - Microsoft MVP
    Dec 4, 2005
  2. Luke

    choosing a package name

    Luke, Sep 8, 2005, in forum: Java
    Replies:
    43
    Views:
    1,325
    Shane Petroff
    Sep 12, 2005
  3. alan
    Replies:
    4
    Views:
    426
  4. Antony

    Choosing GUI Module for Python

    Antony, Nov 9, 2009, in forum: Python
    Replies:
    25
    Views:
    5,274
    Dietmar Schwertberger
    Nov 17, 2009
  5. Javier Lopez

    Choosing a name/codename for your project

    Javier Lopez, Mar 18, 2014, in forum: C Programming
    Replies:
    8
    Views:
    110
    Eric Sosman
    Mar 21, 2014
Loading...

Share This Page