From: Mohammad Khan Date: 2004-11-12T05:41:50+09:00 Subject: Re: true? & false? On Thu, 2004-11-11 at 14:50, Austin Ziegler wrote: > 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, Yes, I wrote it improperly, sorry about that. class Object def true? return self == true end def false? return self == false end end > 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 > You might noticed from my previous posting, the reason to I want to have #true? and #false? Example: a = true b = false using my 'class Object' a.true? -> true b.false? -> true using your 'class Object' a.true? -> false b.false? -> false which are wrong according to my proposed #true? and #false? > 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" I think, its a personal taste. I like "a.nil?" more than "a == nil" same way, I like a.true? than a == true when I consider 'a' in boolean context.. i write . .. " if a" when I need to make sure that 'a' is really a 'true'.. I write a.true?, you might write it a == true. "We live in a free world" ;-) -- Mohammad > > > puts "b is false in boolean context" if not b > > Try "unless b". > > -austin > -- > Austin Ziegler * halostatue@gmail.com > * Alternate: austin@halostatue.ca