From: "Shot (Piotr Szotkowski)" Date: 2010-01-19T19:40:47+09:00 Subject: Re: string to int --xHFwDpU9dbj6ez1V Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Jes=C3=BAs Gabriel y Gal=C3=A1n: > On Tue, Jan 19, 2010 at 11:13 AM, Phillip Curry wrote: >> Is there a nice way to convert a string to an integer if it is in >> fact an integer, and to return nil or even an error if it's not? > Try this: > irb(main):002:0> Integer("12") > =3D> 12 > irb(main):003:0> Integer("fdfdf") > ArgumentError: invalid value for Integer: "fdfdf" > from (irb):3:in `Integer' > from (irb):3 > from :0 > irb(main):005:0> Integer("12ijk") > ArgumentError: invalid value for Integer: "12ijk" > from (irb):5:in `Integer' > from (irb):5 > from :0 =E2=80=A6and if you=E2=80=99d rather get nil, you can try rescuing the exce= ption inline: >> Integer('12') =3D> 12 >> Integer('fdfdf') rescue nil =3D> nil >> Integer('12ijk') rescue nil =3D> nil =E2=80=94 Shot --=20 We have things like protected properties. We have abstract methods. We have all this stuff that your computer science teacher told you you should be using. I don=E2=80=99t care about this crap at all. [Rasmus Lerdorf on PHP] --xHFwDpU9dbj6ez1V Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAktVjCsACgkQi/mCfdEo8UpxGwCeIAplXPz6tjfkpXt/VgLt9xZN tcQAoK/vE16h2Hup3CcJCciIoOxvPHiQ =yGta -----END PGP SIGNATURE----- --xHFwDpU9dbj6ez1V--