From: Cyent Date: 2005-05-07T08:44:24+09:00 Subject: Re: RCR 303: nil should accept missing methods and return nil On Sat, 07 May 2005 02:28:18 +0900, Austin Ziegler wrote: > On 5/5/05, John Carter wrote: > I said this on the RCR comment area, but PDF::Writer is *correct* to > blow up when it gets nil. Without this, there are things that I > could not *possibly* have debugged. With the silent eating of > messages, the generated PDF files would be incorrect -- invalid -- > and there's not a damned thing I could do to tell where the problem > is. > > I don't really know what to say to this -- this will turn a correct > behaviour into a completely invalid behaviour. Ah, but _would_ it. On any other object I would agree with you. But nil occupies a special place in the ruby world. Also, Ruby is a dynamic language, with objects all the way down. nil means in effect, no thing is here. What happens if I tell no thing to do something. No thing happens and I'm left with no thing. So I ask you, next time you are working on your code, when you hit and undefine method on nil, NoMethodError. Do my little trick. Put... class NilClass def method_missing(sym,*args) nil end end in a file nillit.rb, and pull that file into your code with ruby -w -rnillit your_code See what happens. Is your assertion correct? Do you actually generate incorrect PDF? (In which case you are right) Does it just plain work? (In which case I am right) Or does it blow up with an exception or error report somewhen else? (I which case I'm still right.) Whatever happens, try it, and what ever the answer, email me or add a comment to the RCR and I will summarize to the list later. I'm better a beer (to be collected in Christchurch New Zealand) that practical software will find that... a) Mostly it will just do the right thing. b) Otherwise it will do the right thing for now, and be caught later as an Exception or error report. (I suspect by adding a few more things like class NilClass def respond_to?( sym) true end end may cure some of those... c) And very rarely, far more rarely than people suspect, will it silently do the wrong thing. Also as you go along, consider cases where you could just drop conditionals. How many if branches would you be quietly dropping? As for debugging, if you look at the RCR, the implementation I give creates state on nil that records which method was missing and in which calling context. -- John Carter The Cybernetic Entomologist - cyent@xtra.co.nz http://geocities.yahoo.com/cy_ent I'm becoming less and less convinced of humans as rational beings. I suspect we are merely meme collectors, and the reason meme is only kept on to help count our change.