From: Trans Date: 2006-10-07T02:10:12+09:00 Subject: Re: nil being empty ara.t.howard@noaa.gov wrote: > On Fri, 6 Oct 2006, Trans wrote: > > > Ohad Lutzky wrote: > >> Show of hands - who thinks this is bad form? > >> > >> class NilClass > >> def empty?; true; end > >> end > > > > Put your hands down. Notice that everyone that says it's bad form is doing > > so on purely ideological basis. I challenge them to show one good > > _practical_ case where it's truly "bad". And no, purposefully expecting a > > NoMethod error doesn't count --that IS bad form. > > hash_of_lists = Hash.new{|h,k| h[k] = new_list} > hash_of_lists[:key] << 42 if hash_of_lists[:key].empty? That's the best example I have yet seen. Thanks for that Ara. Yet there's still a problem. Your expecting a NoMethodError on nil, just like I said. That's the thing, there's no other possible issue involved here. Now I'll grant James' concern that it can make some bugs harder to track down, but if that's our primary concern then I suggest we get back to static typing. Consider further, that ANY EXTENSION WHATSOEVER can have the same effect. If someone's class X defines #geegee and I add #geegee to String, one can no long expect String to raise a NoMethodError on #geegee. So I argue that the bad form is not in using nil.empty? BUt in expecting such an error, and not addressing it properly. hash_of_lists = Hash.new{|h,k| h[k] = new_list} raise AProperError unless hash_of_lists[:key] hash_of_lists[:key] << 42 if hash_of_lists[:key].empty? T.