From: dblack@... Date: 2006-10-07T02:52:36+09:00 Subject: Re: nil being empty Hi -- On Fri, 6 Oct 2006, Robert Dober wrote: > On 10/6/06, dblack@wobblini.net wrote: >> >> On Fri, 6 Oct 2006, Ohad Lutzky wrote: >> >> > Show of hands - who thinks this is bad form? >> > >> > class NilClass >> > def empty?; true; end >> > end >> >> /me raises hand. > > > I feel this being an unnecessary generalization. > Probably it is very often not the best way to treat the problem, but > it might as easily be the most elegant. > Instead of raising your hand David, please speak out ;) The idea of measuring whether or not nil is "empty" is like measuring whether or not 1001 is "blue", or defining a method called FalseClass#scan(regex). > Now it would be unfair to ask others for their wisdom without delivering my > own (as small as it might be). > > If you define your small little NilClass monkeypatch above because you had > nils where you expected containers, guess what I think of your code ;) > If on the other hand the NilClass Emptyness is integral part of your design, > document it well and go ahead! > Ruby is a Tool not a Religion. (well at least I think so ;) Since Ruby lets you define or not define nil#empty?, not defining it is no more "religious", in the sense I think you mean it, than defining it -- and in general, religious imagery is about as related to programming as empty? is to nil :-) It's just a discussion about best practices. David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org