From: Konrad Meyer Date: 2007-10-15T17:11:36+09:00 Subject: Re: nil.to_i returning zero --nextPart29087866.W4Jvgm8RBk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Quoth Michael Fellinger: > On 10/15/07, Chad Perrin wrote: > > On Mon, Oct 15, 2007 at 04:40:35AM +0900, Gerardo Santana G?mez Garrido= =20 wrote: > > > > > > What does nil.to_i return? Zero. And I wonder why. How can nil be > > > interpreted as a number. It's beyond me. > > > > There's been a few attempts to answer the question already. I'm curiou= s, > > though: > > > > What would you expect it to return, and why would you think that should > > be the expected behavior? > > > > Nothing immediately occurs to me as a better result of evaluating > > nil.to_i than 0, within the context of the Ruby language, but if you ha= ve > > an alternative view I'm open to learning from it. >=20 > nil.to_i > # > NoMethodError: undefined method `to_i' for nil:NilClass > Integer(nil) > # > TypeError: can't convert nil into Integer >=20 > This is on my wish list for Christmas among other things. Thing is, #to_i explicitly does *not* throw errors -- that's what Integer()= is=20 for. Current (1.8.6) ruby doesn't error on Integer(nil) either, but IMO it= =20 should. Regards, =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart29087866.W4Jvgm8RBk Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBHEyCzCHB0oCiR2cwRArhXAJ0agOQaByZVkw75rbKr5wIsLvBFogCfQVo7 640i+Qu+zwpqi4snLv9MdC0= =JpTc -----END PGP SIGNATURE----- --nextPart29087866.W4Jvgm8RBk--