From: Xavier Noria Date: 2003-07-16T02:30:38+09:00 Subject: Re: How to reduce Ruby runtime error? On Tuesday 15 July 2003 19:06, Xiangrong Fang wrote: > My last post seems not appear yet -- usually it appears very fast. So > I repeat myself briefly: > > I want my program to be robust. I don't want to check it after my > program is running in production environment. I want it to be > self-curable, i.e., tolerate these errors. > > Is it possible to "overload" the Nil class so that it is polymophic? > for example, > > if the method upcase is called, nil act like string, if the > expression a = 2 + nil is executed, nil act like zero? That will be > fantastic. This happens in Java as well. Let me use its jargon. One documents whether a returned object can be null or not. For instance the persistence layer's read_user_by_id(id) may decide to indicate that the requested user does not exist returning a null object. And in that case the contract has to say so. In that case, if the caller invokes user.name() without checking whether the readed user object was null or not, that's his fault. Otherwise, a method that returns nulls in a non well specified way might need revision. If a method should return always a string and does not do it, we've got a bug. And as a last resort, you can wrap your application in a big try/catch to avoid aborts in production. -- fxn