From: Austin Ziegler Date: 2004-11-12T04:50:45+09:00 Subject: Re: true? & false? On Fri, 12 Nov 2004 00:28:35 +0900, Mohammad Khan wrote: > The main reason I brought up this issue is: to make > differentiation between "a value in boolean context" and "its real > value" Why? This is the ultimate question that I don't think has been answered at all. Why is #true? and #false superior to explicit tests? Note that I'm not referring to the alternative directions inspired by your post (e.g., #if_true and #if_false), but your suggestion that "foo.true?" is better than "foo == true". > class Object > def true? > return ( (self == true) or (self === TrueClass) ) > end > > def false? > return ( (self == false) or (self === FalseClass) ) > end > end This is improperly written. +true+, +false+, and +nil+ are the only possible values of TrueClass, FalseClass, and NilClass, respectively. Thus, self == true and TrueClass === self (note the inversion of parameters on the call to #===) are the same test. You can more efficiently write what you want as: class Object def true? false end def false? false end end class TrueClass def true? true end end class FalseClass def false? true end end (This is, more or less, how nil? is written. Object#nil? returns false; NilClass#nil? returns true.) > so.. again for the above examples: > puts "a is true in boolean context" if a > puts "a is really a *true*" if a.true? Why is this better than "if a == true" > puts "b is false in boolean context" if not b Try "unless b". -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca