From: Hal Fulton Date: 2004-11-12T08:13:04+09:00 Subject: non-nil, etc. (was Re: true? & false?) Gavin Sinclair wrote: >>unless a.nil? >>.... >>end > > if a.notnil? > ... > end > > :) You put a smiley, but in all seriousness I've thought of this. (Actually I'd use nonnil? or nnil?) In fact, when I'm writing code that no one else will see, I sometime use methods like these: a1 = [] a2 = [1,2,3] s1 = "" s2 = "a string" h1 = {} h2 = {1=>2, 2=>4} a1.null? # true a1.nnull? # false a2.null? # false a2.nnull? # true s1.null? # true h1.null? # true # And so on... See below, and let the flamefest begin. Hal class Array def null? ! empty? end end class String def null? ! empty? end end class Hash def null? ! empty? end end class Fixnum def null? self == 0 end end class File def null? # now a little fun size == 0 end end # And now even more fun class Nil def null? true end def nnil? # for symmetry false end end # And the most fun of all, our beloved nnull? class Object def null false # until overridden end def nnull? ! null? # inherited by everyone end end