From: Michel Martens Date: 2005-02-22T23:58:40+09:00 Subject: Re: Proposal for nil, 0, and "" in an if statement Even though I'm not sure I love globals like $\ or $*, I think the "rule of validity" method should depend of a variable of that kind. For instance, a global $- could be defined and default to [nil, false, "", 0, [], {}]. That way, one could define at runtime wich values can be declared "vapid" or "empty" or whatever the final method name is. Example: price = 2 discount = 2 total = '%0.2f' %(price - discount) $- << '0.00' if total.valid? # Will never get here end On Tue, 22 Feb 2005 23:37:37 +0900, Gavin Kistner wrote: > On Feb 22, 2005, at 7:32 AM, Gavin Kistner wrote: > > > On Feb 22, 2005, at 7:19 AM, Peter Hickman wrote: > >>> I don't have actual code, but imagine looping through records > >>> returned with financial data, and you want to print out a "-" if the > >>> field is missing -or- zero. The whole "this field doesn't have any > >>> meaningful information that contributes to the sum of the column" > >>> spreadsheet thing. > >>> > >> Might this not cause trouble with databases. A database record > >> (returned > >> as a structure or object) would have NULL == nil for fields with no > >> value which is not the same as a field with a definite value such as > >> 0. > > > > Yes, there are many cases where you DO want to distinguish between nil > > and 0. These cases are already handled by the current behavior of > > Ruby. > > To be clear - the proposal is NOT to make Ruby act like one of the many > other languages where "", 0 and friends are treated as non-truth values > in boolean expressions. The proposal is simply to add an additional > method to all that allows a programmer to treat them the same in the > few cases where that is desirable. > >