From: Nucleon Date: 2003-07-16T07:43:22+09:00 Subject: Re: How to reduce Ruby runtime error? On Wed, 16 Jul 2003 02:06:53 +0900, 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. > > 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. > > Thanks for your help. > > Shannon class Object def method_missing(id, *args) STDERR.puts "Missing method #{self.class}##{id}(#{args.collect{|i|i.inspect}.join(", ")}) called from #{caller[0]}" return nil end end Now, whenever you try to call a non-existant method, you'll get an informative complaint, but the call will succeed and return nil. This is evil, of course, and you really should fix the problem, but it will work. For example: nil.upcase nil + 2 outputs: Missing method NilClass#upcase() called from (irb):8:in `irb_binding' Missing method NilClass#+(2) called from (irb):9:in `irb_binding' -- Nucleon, RLU #278930, If you destroy me, I shall become more powerful than you can possibly imagine. -- Obi-Wan