From: "Mauricio Fernández" Date: 2003-04-07T18:36:10+09:00 Subject: Re: To inherit or to include? That is the question. On Mon, Apr 07, 2003 at 04:47:25PM +0900, Robert Klemme wrote: > > So, the module Foo was able to introduce some state into the class Bar. > > Oops! Something learned again. Obviously I misread something. Maybe the > difference then is that a module can't introduce state on initialization? > ??? batsman@kodos:/tmp$ expand -t 2 a.rb module A def initialize(*args) args.each do |a| puts "Adding i.v. #{a}" instance_eval "@#{a} = 1" end end end class B include A end a = B.new("a") puts a.instance_variables batsman@kodos:/tmp$ ruby a.rb Adding i.v. a @a The more I think about it, the more it seems to me that the following are the only differences: * a module cannot be instantiated * there is no relation between the singleton method of a class and that of an included module (as opposed to what happens in normal inheritance) * a module cannot be inherited from ;) We can then choose between classes and modules to model different designs, but as far as the interpreter is concerned these seems to be the only differences (gotta check the source). -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Linux: because a PC is a terrible thing to waste -- ksh@cis.ufl.edu put this on Tshirts in '93