From: Philipp Meier Date: 2002-09-20T02:00:35+09:00 Subject: Re: subclassing Integer --yrj/dFKFPuw6o+aM Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Sep 20, 2002 at 01:20:35AM +0900, John Tromp wrote: > I'm writing a state space search program where each state can be consisely > described by an Integer. I need these states to have several new methods. >=20 > I can either > 1) create a new class with an Integer attribute > or > 2) try to subclass Integer Subclassing might give you undesired effects with return types. Example: class Cart < Array def total sum =3D 0 each do |product| sum +=3D product.price end end end class Product attr_reader :price def initialize(price) @price=3Dprice end end c =3D Cart.new() c << Product.new(5) c << Product.new(10) c.total # =3D> 15 c +=3D [Product.new(7)] c.total # NameError: undefined method `total' for # -billy. --=20 Meisterbohne S=F6flinger Stra=DFe 100 Tel: +49-731-399 499-0 eL=F6sungen 89077 Ulm Fax: +49-731-399 499-9 --yrj/dFKFPuw6o+aM Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE9igLhfBriNoqItSYRAhsvAJ95LRX5znmIESRnonuF/b23UEZ6IACfc0iy 8jv8RzVVPOQ3dkZjtDPesk4= =V1iH -----END PGP SIGNATURE----- --yrj/dFKFPuw6o+aM--