From: Andrew Stone Date: 2007-12-12T09:29:27+09:00 Subject: Re: opposite .nil? ------=_Part_32403_2994919.1197419321685 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Dec 11, 2007 6:27 PM, Gary Wright wrote: > > On Dec 11, 2007, at 3:03 PM, Andrew Stone wrote: > > Sure, I was just looking for a more readable construct. Thanks > > again for > > your reply. > > I'd suggest not_nil? > > class Object > def not_nil? > !self.nil? > end > end > > exists? or not_nil? are both awkward if you actually have > a reference to false: > > condition = (2 > 3) # condition is false > > if condition.exists? > # this branch will run because condition is not nil > else > # do something else > end > > I think it would be clearer to be explicit about your intent > if you really want to bundle up false with true-behaving > objects: > > if condition or (condition == false) > # do something > end > > > In practice, I've rarely come across a situation where I wanted > to treat a false reference the same as a reference to a 'real' > object. > > Gary Wright > > I definitely wanted a true opposite of .nil? and I do agree the term exists? is a little awkward, but it was the first term that people seem to use when thinking of !nil?. btw, this was not the result of some huge internet poll with thousands of responses, just 4 people, so...eh, it was good enough for me. Phrogz mentioned the possibility of adding it to the FalseClass as well, but that's not my intention for this method. False is a value just like an empty string is a value where nil is the absence of value. Now I know some will argue that the absence of a value is a value. True and fair enough and a reason why the exists? method wouldn't be added to the False class to return a false value. false.exists? == true. I don't run across this scenario often. Today was just one of those days that I decided to look a little further into the possibilities. Gary and Robert, thanks for the responses, andy -- Andrew Stone ------=_Part_32403_2994919.1197419321685--