From: Artur Merke Date: 2003-11-19T18:52:17+09:00 Subject: Re: to_i Hi, you don't really want to compare ruby with c! BTW, what you have overlooked is the fact, that c doesn't have a nil return value (0 is only a nil value in connection with pointers, not integers), so Kernighan & Richie havn't had another option then to return 0, but in ruby you have a nil value, and I'm pretty sure ;-), K&R would have chosen to return nil in ruby's to_i method. Artur On Tue, 18 Nov 2003, Josef 'Jupp' SCHUGT wrote: > Hi! > > * Artur ; 2003-11-17, 14:20 UTC: > > IMO, it has to do with the principle of least surprise, and it is a > > BIG surprise to get > > "hello".to_i -> 0 > > Let me quote Kernighan & Richie, Programming in C (naive > implementation of atoi): > > int atoi(char s[]) > { > int i, n; > > n = 0; > for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i) > n = 10 * n + (s[i] - '0'); > return n; > } > > Assuming this sample implementation it is not at all surprising that > atoi returns 0 if the argument string does not start with a 'digit'. > Given Ruby's C heritage it is no surprise that to_i behaves the same > as atoi. > > Josef 'Jupp' Schugt > -- > .-------. > message > 100 kB? / | | > sender = spammer? / | R.I.P.| > text = spam? / ___| |___ > >