From: David Vallner Date: 2006-11-17T23:24:13+09:00 Subject: Re: [Helppp xD] Newbie issue --------------enig135529D0A634041B75FA5E47 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Flaab Mrlinux wrote: > Hi all!! >=20 > I've declared a method inside the string class that has to answer the=20 > amount of "X" and "2" chars in a string. The string is ALWAYS gonna be = > 15 chars size. The code is the following: >=20 > class String >=20 > # DAME VARIANTES > def variantes > @variantes =3D 0 > 15.times do |@cont| > if (self[@cont] =3D=3D "2" || self[@cont] =3D=3D "X") > @variantes =3D @variantes + 1 > end > end >=20 > return @variantes > end > end >=20 > cadena =3D "XX222XX222XX1X2" > puts cadena.variantes >=20 > And the last line ALWAYS ANSWERS a CERO. Why? Why it doesn't enter the = > if statement inside the method? >=20 > Thx >=20 >=20 String#[] returns the integer code of the character at that position. So, either use =09 if(self[@cont] =3D=3D ?2 || self[@cont] =3D=3D ?X) or if(self[@cont..@cont] =3D=3D "2" || self[@cont..@cont] =3D=3D "X") Also: 1. You shouldn't use instance variables when local ones will suffice. I.e. use "variantes" and "cont" instead of "@variantes" and "@cont" 2. Please send code snippets with identifiers in English - it takes a little guesswork out of what you're trying to achieve. 3. Smileys and netspeak are fluff and a little out of place for the medium. Cut out the "xD" and use "Thanks" instead of Thx, please. David Vallner --------------enig135529D0A634041B75FA5E47 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) iD8DBQFFXcX/y6MhrS8astoRAjVpAJwNfCSv422RY97S95l1bkBKRDZOsQCfZIYP bcaz45d1Dx0wi4OEdTVTRRQ= =XDuT -----END PGP SIGNATURE----- --------------enig135529D0A634041B75FA5E47--