From: Simon Strandgaard Date: 2003-11-17T10:23:33+09:00 Subject: Re: to_i On Mon, 10 Nov 2003 17:44:36 +0100, Robert Klemme wrote: > > "Artur Merke" schrieb im Newsbeitrag > news:Pine.LNX.4.33.0311101726040.17966-100000@as0203.cs.uni-dortmund.de... [snip] > Why doesn't "hello".to_i deliver a nil as a return value, which can > clearly be recognized as a conversion failure? > > IMHO this is a struggle between convenience and cleanness. I guess > Matz decided for convenience in this case, maybe because in most cases > using 0 is ok. If you want to do correct input checking you do > /^[+-]?\d+$/ =~ foo anyway. Something like this: server> expand -t2 c.rb def atoi(str) return str.to_i if /^\d+$/ =~ str raise "Cannot convert" end p atoi("42") #-> 42 #p atoi("i18n") #-> exception #p atoi("0.42") #-> exception server>