From: David Vallner Date: 2006-11-13T02:18:17+09:00 Subject: Re: Help convert a Perl user to the Ruby Way. --------------enig38CC852C2FED546CA91A3E9B Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Sebastian Reid wrote: > Hi all. >=20 > I've been working on some Ruby projects using the Rails framework > and getting on well, however recently I've started on some projects > using Ruby alone and have run into some issues as the task gets a littl= e > more complicated. >=20 > One particular script is giving me trouble and I suspect it is due > to my Perly ways. Below is a pastebin of the code. As some I'm sure > can tell, it is an rbot plugin that right now doesn't do a whole lot. >=20 > http://pastebin.com/822611 >=20 > The highlighted line is flagging up the error: >=20 > TypeError: no implicit conversion from nil to integer >=20 > on line 18 (highlighted). >=20 > Now this is looking to me like a variable instantiation issue which= > brings us back to Perl. In Perl that line would be perfectly legal > (though some would debate its elegance) and hashes and arrays would be > created as and when required. >=20 In Ruby, only instance variables are autovivified to nil, and that by itself is a behaviour that gets on my nerves (typo-prone). =46rom the code it looks @registry should be an object (maybe use a Struct) instead of a Hash, and you should preinitialise its contents to empty arrays and whatnot in its initialize method. >=20 > I'm willing to accept that this may not be the best way to do thing= s > in Ruby, thus I come to you to ask for a quick rundown on best practice= s > in these situations. >=20 > I've played around with default procs and the like in the > initialisation routine to no avail. =20 Using blocks to initialize hashes / arrays is fine when not nested, otherwise things get messy. Clean up your data structure initialisation to be explicit? David Vallner --------------enig38CC852C2FED546CA91A3E9B Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) iD8DBQFFV1eAy6MhrS8astoRAl2JAJ0R3f8MDV/VCx2/OhT+1Mxag1vAcgCfRfDG ZpGORMhRTE0zP3StHYb3oNU= =PG3T -----END PGP SIGNATURE----- --------------enig38CC852C2FED546CA91A3E9B--