From: Austin Ziegler Date: 2002-11-09T03:49:17+09:00 Subject: Re: understanding modules/classes On Sat, 9 Nov 2002 02:53:30 +0900, Eric Schwartz wrote: > "Gavin Sinclair" writes: >> It is perfectly good style to include a module in a class. That >> is one of the primary aims of modules: in this sense, they are >> called mix-ins. > I'm afraid you've confused me further. Let me illustrate. > > module TestHarness > class TestTableModel > def initialize(project) > @project = project > # stuff happens > end > def MachineList > # return a list of machines that have test results for the > # current project > end > #... other methods defined, too. > end > end > This is the Model of an MVC pattern I'm using to display test > results from a CGI program. > > Now, say I'm writing the CGI itself. Inside it, do I say: > require 'testharness/testtablemodel' > require 'testharness/testtableview' > include TestHarness IMO, this is what you want. I may prefer to do: foo = TestHarness::TestTableModel.new That is, I might have a TestTableModel in my own code. I could also choose to do: require 'testharness/testtablemodel' class Tester include TestHarness ... end Then Tester has become an alias -- of sorts -- for TestHarness. module Foo class Bar def baz puts "in Foo::Bar::baz" end end end class Bag include Foo def initialize @bar = Bar.new end def baz puts "in Bag::baz" @bar.baz end end t = Foo::Bar.new t.baz # in Foo::Bar::baz b = Bag.new b.baz # in Bag::baz # in Foo::Bar::baz z = Bag::Bar.new z.baz # in Foo::Bar::Baz include Foo c = Bar.new c.baz # in Foo::Bar::Baz -austin -- Austin Ziegler, austin@halostatue.ca on 2002.11.08 at 13.38.27