# Opposite .nil?

**URL:** <https://www.ruby-forum.com/t/opposite-nil/122873>\
**Category:** Ruby\
**Created:** [December 12, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/opposite-nil/122873 "2007-12-12T19:09:05Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 12, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/opposite-nil/122873/1 "2007-12-12T19:09:05Z")

</div>

I’ve looked around, but could not find a method that is the opposite of  
.nil?. I know there is !my\_var.nil?, but I think my\_var.exists? is more  
readable.

I wanted to throw this out there before I added the method in myself.

thanks,  
andy

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 12, 2007, 11:44pm UTC](https://www.ruby-forum.com/t/opposite-nil/122873/2 "2007-12-12T23:44:30Z")

</div>

Andrew S. wrote:

> I’ve looked around, but could not find a method that is the opposite of  
> .nil?. I know there is !my\_var.nil?, but I think my\_var.exists? is more  
> readable.
> 
> I wanted to throw this out there before I added the method in myself.
> 
> thanks,  
> andy

if not my\_var.nil?

is the same as

if my\_var

_unless_ my\_var is false.

-Justin

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 1:03am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/3 "2007-12-13T01:03:03Z")

</div>

if not my\_var.nil?

> 

Thanks Justin. I should have been more clear with the usage.

def should\_this\_be\_done?  
am\_I\_sure? && my\_object.var && !my\_object.var.nil?  
end

I just think it would read better if the last test was  
my\_object.var.exists?

thanks,  
andy

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 1:03am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/4 "2007-12-13T01:03:04Z")

</div>

> 1. No such opposite-of-nil? method exists. If you want it, add it.

Thanks for actually answering my question. Much appreciated.

1. In the particular example above (which I realize is just an

> example) you could write that as:
> 
> def should\_this\_by\_done?  
> am\_I\_sure? unless !my\_object || my\_object.var.nil?  
> end
> 
> But, of course, that just moves the negation.

Sure, I was just looking for a more readable construct. Thanks again  
for  
your reply.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 1:03am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/5 "2007-12-13T01:03:03Z")

</div>

On Dec 11, 11:56 am, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I just think it would read better if the last test was my\_object.var.exists?

1. No such opposite-of-nil? method exists. If you want it, add it.

class Object  
def exists?  
true  
end  
end

class NilClass  
def exists?  
false  
end  
end

# up to you if you want this for FalseClass, too

1. In the particular example above (which I realize is just an  
example) you could write that as:

def should\_this\_by\_done?  
am\_I\_sure? unless !my\_object || my\_object.var.nil?  
end

But, of course, that just moves the negation.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 3:24am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/6 "2007-12-13T03:24:40Z")

</div>

> def should\_this\_be\_done?  
> am\_I\_sure? && my\_object.var && !my\_object.var.nil?  
> end

Ooops… meant

def should\_this\_be\_done?  
am\_I\_sure? && my\_object && !my\_object.var.nil?  
end

Didn’t mean to put add .var on the second test.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 1:49am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/7 "2007-12-13T01:49:16Z")

</div>

On Dec 11, 2007, at 3:03 PM, Andrew S. 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 W.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 3:57am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/8 "2007-12-13T03:57:34Z")

</div>

On Dec 11, 12:56 pm, “Andrew S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## thanks, andy
> 
> Andrew S.

There’s no reason to say myobject.var && !my.object.var.nil? Any  
object that passes the first test is not nil.

The only reason to check !obj.nil? is if you want false to be an  
accepted value.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 3:56am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/9 "2007-12-13T03:56:58Z")

</div>

On Dec 11, 3:03 pm, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> 
> > But, of course, that just moves the negation.
> 
> Sure, I was just looking for a more readable construct. Thanks again for  
> your reply.
> 
> –  
> Andrew S.

what about

def should\_this\_be\_done?  
am\_1\_sure? && my\_object.var && my\_object.var  
end

KISS

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 5:19am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/10 "2007-12-13T05:19:27Z")

</div>

> There’s no reason to say myobject.var && !my.object.var.nil? Any  
> object that passes the first test is not nil.

Please see the followup email I sent.

thanks,  
andy

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 4:29am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/11 "2007-12-13T04:29:42Z")

</div>

On 11.12.2007 20:00, Andrew S. wrote:

> am\_I\_sure? && my\_object && !my\_object.var.nil?  
> end
> 
> Didn’t mean to put add .var on the second test.

Well, you could do

am\_I\_sure? && my\_object && my\_object.var

It’s not exactly the same but if you don’t care whether my\_object.var is  
nil or false or if you know that it never will be false then it’s ok.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 5:19am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/12 "2007-12-13T05:19:27Z")

</div>

On Dec 11, 2007 6:27 PM, Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> !self.nil?
> else
> 
> ```
> 
> 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 W.

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

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 5:19am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/13 "2007-12-13T05:19:27Z")

</div>

On Dec 11, 2007 7:10 PM, Ken B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 1. In the particular example above (which I realize is just an  
> > for your reply.
> 
> You’re not forced to program Ruby like it’s C.
> 
> –Ken
> 
> –  
> Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.  
> [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/) [http://www.iit.edu/~kbloom1/](http://www.iit.edu/%7Ekbloom1/)

🙂 very true Ken. I come from Java land myself and I guess old habits  
die  
hard.

thanks for the response,  
andy

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 11:55am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/14 "2007-12-13T11:55:28Z")

</div>

On Dec 11, 7:02 pm, Ken B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here’s a more readable construct:

Hmm… maybe ‘readable’ is in the eye of the beholder 😉

> def should\_this\_be\_done?  
> am\_I\_sure? and my\_object and not my\_object.var.nil?  
> end

Folks should be aware of the difference in precedence. In particular,  
&& has a higher precedence than =, but ‘and’ has a lower precedence  
than =.

> You’re not forced to program Ruby like it’s C.

Agreed

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 5:19am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/15 "2007-12-13T05:19:27Z")

</div>

On Tue, 11 Dec 2007 15:03:53 -0500, Andrew S. wrote:

> > def should\_this\_by\_done?  
> > am\_I\_sure? unless !my\_object || my\_object.var.nil?  
> > end
> > 
> > But, of course, that just moves the negation.
> 
> Sure, I was just looking for a more readable construct. Thanks again  
> for your reply.

def should\_this\_be\_done?  
am\_I\_sure? && my\_object && !my\_object.var.nil?  
end

Here’s a more readable construct:

def should\_this\_be\_done?  
am\_I\_sure? and my\_object and not my\_object.var.nil?  
end

You’re not forced to program Ruby like it’s C.

–Ken

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [December 13, 2007, 11:57am UTC](https://www.ruby-forum.com/t/opposite-nil/122873/16 "2007-12-13T11:57:14Z")

</div>

Facets has #non\_nil? and #not\_nil?. It also has #val? for not nil or  
false or not empty if enumerable. Not to mention #false? and #true?

T.
