From: Piotr Szotkowski Date: 2011-01-05T14:07:50+09:00 Subject: replacing inheritance with module inclusion, oh my --ikeVEW9yuYc//A+q Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable The problem: a Person should be an OpenStruct-like Entity, with the Entity being taking care of the Person=E2=80=99s OpenStruct-ness and persis= tence (say, for starters, the Person being added to a pool of Entities). Tentative approach with inheritance: require 'ostruct' class Entity < OpenStruct def initialize *args puts 'Entity#initialize' super puts "adding #{self} to pool" end end class Person < Entity def initialize *args puts 'Person#initialize' super end end Person.new name_prefix: 'Dr', given_names: ['Horace'], surname: 'Worblehat' Result: Person#initialize Entity#initialize adding # to pool The problem: how about making Entity a module so not to require Person to inherit from it. Something like the below: require 'ostruct' module Entity def initialize *args puts 'Entity#initialize' super puts "adding #{self} to pool" end end class Person < OpenStruct include Entity def initialize *args puts 'Person#initialize' super end end Person.new name_prefix: 'Dr', given_names: ['Horace'], surname: 'Worblehat' Result: Person#initialize Entity#initialize adding # to pool The real problem: how to move the OpenStruct-ness of Entities away from Person and into the Entity? You can=E2=80=99t include/extend Entity into/wi= th OpenStruct (because OpenStruct is not a module) and you can=E2=80=99t make Entity inherit from OpenStruct (because Entity is a module). I=E2=80=99m fine with, say, any Entity.new + Entity.included (not-really-)magic required or some such, I just can=E2=80=99t seem to be able to put my finger on the needed wiring=E2=80=A6 :) (A cherry on the top would be if Person#initialize did not have to remember to call super; I want Person to be as simple as possible and offload as much as possible to Entity.) =E2=80=94 Piotr Szotkowski --=20 =E2=80=98The Ruby community should proceed with all deliberate speed towards ISO standardization of the language.=E2=80=99 =E2=80=98Yeah, look what it did to Forth.=E2=80=99 =E2=80=98Don=E2=80=99t just say it, show it: =E2=80=99 [M. Edward (Ed) Borasky, Matt Lawrence, Gregory Brown, ruby-tal= k] --ikeVEW9yuYc//A+q Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iEYEARECAAYFAk0j/J8ACgkQi/mCfdEo8Uqb2wCfV/Dm2LPUCxAS5W07hd/z9D+s Q1MAnjqoeef0DFxVLa9Mbk19Qotx2JOK =+JQl -----END PGP SIGNATURE----- --ikeVEW9yuYc//A+q--