From: "Mauricio Fernández" Date: 2003-04-05T00:01:07+09:00 Subject: Re: To inherit or to include? That is the question. On Fri, Apr 04, 2003 at 10:51:02PM +0900, Jim Freeze wrote: > I've been poking around the mailing list looking for > some insight into using inheritance vs module include. [...] > I cannot see that the two behave that differently. > I'm sure there are some differences. Maybe someone > can help me enumerate them. > > Ideally, I would like some set of guidelines to help > me make a choice between these two. matz already stated that inheritance in Ruby is little more than implementation reuse... no wonder we can mix modules and classes (no pun intended) so easily... So it all comes down to the following: what's the difference between an inherited abstract base class and an included module? There's not really that much (if you omit syntactic issues), but here's something I can think of now: batsman@kodos:/tmp$ expand -t2 a.rb module Foo; def self.bar; puts "Foo.foo" end end class Bar; def self.bar; puts "Bar.foo" end end class A < Bar end class B include Foo end A.bar B.bar batsman@kodos:/tmp$ ruby a.rb Bar.foo a.rb:13: undefined method `bar' for B:Class (NameError) i.e. there's no relationship between singleton classes w/ include. Another thing would be p B === B.new p Bar === B.new true false There must be more but I am low on caffeine today :-P Wait for Guy Decoux's answer ;-) -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Even more amazing was the realization that God has Internet access. I wonder if He has a full newsfeed? -- Matt Welsh