Include into class after instance is created

Discussion in 'Ruby' started by Brian Schroeder, May 20, 2004.

  1. Hello,

    it's me again with another question. I want to include a module only after
    having instantiated a class. Something like this

    File: german.rb
    Module Index
    Title = 'Meine Seite'
    end

    File: english.rb
    Module Index
    Title = 'My Page'
    end

    file index.rb:
    class Index < Website
    def initialize
    super
    include(Index)
    end
    end

    where Website requires either german.rb or english.rb.

    But I can't call include as a function. Ruby complains that it is a
    private function of class.

    ../index.rb:10:in `initialize': private method `include' called for Index:Class (NoMethodError)
    from ./index.rb:27:in `new'
    from ./index.rb:27

    Is this scheme possible, am I totally on the wrong track or just missing
    something obvious?

    Regards,

    Brian

    --
    Brian Schröder
    http://www.brian-schroeder.de/
    Brian Schroeder, May 20, 2004
    #1
    1. Advertising

  2. Brian Schroeder

    Kent S. Guest

    Use Object#extend instead of include.

    Cheers,
    Kent.

    On May 20, 2004, at 9:48 AM, Brian Schroeder wrote:

    > Hello,
    >
    > it's me again with another question. I want to include a module only
    > after
    > having instantiated a class. Something like this
    >
    > File: german.rb
    > Module Index
    > Title = 'Meine Seite'
    > end
    >
    > File: english.rb
    > Module Index
    > Title = 'My Page'
    > end
    >
    > file index.rb:
    > class Index < Website
    > def initialize
    > super
    > include(Index)
    > end
    > end
    >
    > where Website requires either german.rb or english.rb.
    >
    > But I can't call include as a function. Ruby complains that it is a
    > private function of class.
    >
    > ../index.rb:10:in `initialize': private method `include' called for
    > Index:Class (NoMethodError)
    > from ./index.rb:27:in `new'
    > from ./index.rb:27
    >
    > Is this scheme possible, am I totally on the wrong track or just
    > missing
    > something obvious?
    >
    > Regards,
    >
    > Brian
    >
    > --
    > Brian Schröder
    > http://www.brian-schroeder.de/
    >
    Kent S., May 20, 2004
    #2
    1. Advertising

  3. On Thu, 20 May 2004 23:05:10 +0900, Kent S. wrote:

    > Use Object#extend instead of include.


    Thanks for the tip.

    I need the constants defined in the module to be included in my class, it
    seems that extend only includes the instance methods.

    $ ri extend
    ---------------------------------------------------------- Object#extend
    obj.extend(module, ...) => obj
    ------------------------------------------------------------------------
    Adds to _obj_ the instance methods from each module given as a
    parameter.

    Any thoughts about this?

    Regards,

    Brian

    --
    Brian Schröder
    http://www.brian-schroeder.de/
    Brian Schroeder, May 20, 2004
    #3
  4. Brian Schroeder

    Kent S. Guest

    How about this:

    $ irb
    irb(main):001:0> module M1
    irb(main):002:1> Const = 1
    irb(main):003:1> end
    => 1
    irb(main):004:0> module M2
    irb(main):005:1> Const = 2
    irb(main):006:1> end
    => 2
    irb(main):007:0> class A
    irb(main):008:1> def initialize(mod)
    irb(main):009:2> self.class.class_eval{ include mod }
    irb(main):010:2> end
    irb(main):011:1>
    irb(main):012:1*
    irb(main):013:1* def const
    irb(main):014:2> puts Const
    irb(main):015:2> end
    irb(main):016:1> end
    => nil
    irb(main):017:0> A.new(M1).const
    1
    => nil
    irb(main):018:0> A.new(M2).const
    2
    => nil
    irb(main):019:0>


    Cheers,
    Kent.
    On May 20, 2004, at 10:33 AM, Brian Schroeder wrote:

    >
    > On Thu, 20 May 2004 23:05:10 +0900, Kent S. wrote:
    >
    >> Use Object#extend instead of include.

    >
    > Thanks for the tip.
    >
    > I need the constants defined in the module to be included in my class,
    > it
    > seems that extend only includes the instance methods.
    >
    > $ ri extend
    > ----------------------------------------------------------
    > Object#extend
    > obj.extend(module, ...) => obj
    > -----------------------------------------------------------------------
    > -
    > Adds to _obj_ the instance methods from each module given as a
    > parameter.
    >
    > Any thoughts about this?
    >
    > Regards,
    >
    > Brian
    >
    > --
    > Brian Schröder
    > http://www.brian-schroeder.de/
    >
    Kent S., May 20, 2004
    #4
    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. Sridhar R
    Replies:
    14
    Views:
    1,381
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Edwin Knoppert
    Replies:
    0
    Views:
    314
    Edwin Knoppert
    Dec 14, 2006
  3. Ami
    Replies:
    3
    Views:
    406
    Tomás
    Feb 27, 2006
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    881
    Andreas Bogenberger
    Feb 22, 2008
  5. Martin P. Hellwig
    Replies:
    1
    Views:
    362
    Martin P. Hellwig
    Mar 26, 2010
Loading...

Share This Page