From: james_b Date: 2003-07-16T03:15:46+09:00 Subject: Re: How to reduce Ruby runtime error? Xiangrong Fang wrote: > Hi John, > > 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. As has been mentioned before, unit testing is highly reccomended. It helps you determine that your code will a) "do the right thing" for a range of allowable input, and b) properly react to certain border conditions where throwing an exception would be appropriate. > > 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. If nil is an acceptable input value then you could use to_s or to_i to get the desired type. s = some_value.to_s.upcase or a = 2 + some_value.to_i But be mindful that this will hide any nil input; if you're OK with that then go ahead. (Actually, it hides more than just nil.) Also, take to heart what others have said about changing the default behavior of core Ruby objects. If you are *adding* a method to Nil then it is unlikley to inadvertently affect other code, but if Nil no longer behaves like Nil, then you have to deal with POGS (Principle of Guaranteed Surprise). James > > Thanks for your help. > > Shannon > >