From: Bill Atkins Date: 2005-04-21T00:18:12+09:00 Subject: Re: [NEWBIE]: Example from Pickaxe2 gives errors... (p. 31) ------=_Part_6379_14909240.1114010266294 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Song doesn't have a constructor defined in that example. When you call=20 Blah.new, Ruby creates a new instance of Blah and then calls the method=20 initialize on that instance. If you had: class Song attr_writer :name, :artist, :duration def initialize name, artist, duration @name, @artist, @duration =3D name, artist, duration end end then calling Song.new("Title", "Artist", 56) would give you an object with= =20 the appropriate values set. When you call Song.new(param1, param2, param3),= =20 Ruby tries to pass those three parameters to the initialize method in Song.= =20 But since initialize doesn't exist, it calls the default initialize, which= =20 takes no parameters - that explains the error you got. Hope that helps. On 4/20/05, Simon.Mullis@equinoxsolutions.com < Simon.Mullis@equinoxsolutions.com> wrote: >=20 > Greeting all, >=20 > How about a refreshingly easy question for a change.... >=20 > Really, really basic question I know, but: >=20 > class Song > attr_writer :name, :artist, :duration > end > =3D> nil >=20 > song =3D Song.new("Blueberry Hill", "Fats Waller", 320) > =3D> ArgumentError: wrong number of arguments (3 for 0) > from (irb):24:in `initialize' > from (irb):24:in `new' > from (irb):24 >=20 > However.... >=20 > song =3D Song.new() > =3D> # >=20 > song.name =3D "Blueberry Hill" > =3D> "Blueberry Hill" >=20 > song.name > =3D> "Blueberry Hill" >=20 > Have I misread / misunderstood the text? Why the errors? >=20 > SM >=20 > (Only 770 pages to go... Woohoo!) >=20 >=20 > -------------------------------------------------------------------------= ----------------- > Equinox Converged Solutions > Tel: +44 (0)1252 405 600 > http://www.equinoxsolutions.com > Equinox Converged Solutions is a trading name of Synetrix Holdings=20 > Limited. >=20 > IMPORTANT NOTICE: > This message is intended solely for the use of the Individual or=20 > organisation to whom it is addressed. It may contain privileged or=20 > confidential information. If you have received this message in error, ple= ase=20 > notify the originator immediately. > If you are not the intended recipient, you should not use, copy, alter, o= r=20 > disclose the contents of this message. All information or opinions expres= sed=20 > in this message and/or any attachments are those of the author and are no= t=20 > necessarily those of Synetrix Holdings Limited. > Synetrix Holdings Limited accepts no responsibility for loss or damage=20 > arising from its use, including damage from virus. >=20 > -------------------------------------------------------------------------= ------------------ >=20 >=20 --=20 Bill Atkins ------=_Part_6379_14909240.1114010266294--