# Coercing nil/obj to false/true

**URL:** <https://www.ruby-forum.com/t/coercing-nil-obj-to-false-true/185991>\
**Category:** Ruby\
**Created:** [March 30, 2010, 6:37am UTC](https://www.ruby-forum.com/t/coercing-nil-obj-to-false-true/185991 "2010-03-30T06:37:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveybaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steveybaby](https://www.ruby-forum.com/u/steveybaby)\
**Post date:** [March 30, 2010, 6:37am UTC](https://www.ruby-forum.com/t/coercing-nil-obj-to-false-true/185991/1 "2010-03-30T06:37:53Z")

</div>

I know this probably comes up a lot, but I could not Google the exact  
question as I’m formulating it.

Is there a way to avoid this idiom?

def is\_there\_one(whatever)  
!some\_call\_to\_lib\_that\_returns\_object\_or\_nil(whatever).nil?  
end

The above code tersely coerces a result to a boolean value, but it’s  
hard to read, because it’s a double negative, and the “!” negative is  
lexically separated from the “nil?” negative.

I occasionally do this instead:

x = some\_call\_to\_lib\_that\_returns\_object\_or\_nil  
x.nil?

That’s more readable for me, but more verbose, and I can never decide  
what to name “x.”

I can add something like this to my codebase, but I’m wondering if  
something similar is already built in:

def to\_boolean x  
return !x.nil?  
end

def is\_there\_an\_x\_like(whatever)  
to\_boolean(some\_call\_to\_lib\_x\_that\_returns\_object\_or\_nil(whatever))  
end

def is\_there\_a\_y\_like(whatever)  
to\_boolean(some\_call\_to\_y\_lib\_that\_returns\_object\_or\_nil(whatever))  
end

Finally, I can just have my method return obj or nil, and let the  
callers use it in boolean expressions, but my fear there is that I set  
the wrong expectation for the caller, if I later return a different  
object, when my intention is only to indicate whether a query succeeds  
or not. (You guessed it, this is motivated by ActiveRecord, but it’s  
really a Ruby question.)

Thoughts?

---

<div class="post-metadata">

**Author:** ![steveybaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steveybaby](https://www.ruby-forum.com/u/steveybaby)\
**Post date:** [March 30, 2010, 9:18am UTC](https://www.ruby-forum.com/t/coercing-nil-obj-to-false-true/185991/2 "2010-03-30T09:18:14Z")

</div>

2010/3/30 Steve H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Is there a way to avoid this idiom?
> 
> def is\_there\_one(whatever)  
> !some\_call\_to\_lib\_that\_returns\_object\_or\_nil(whatever).nil?  
> end

> Finally, I can just have my method return obj or nil, and let the  
> callers use it in boolean expressions, but my fear there is that I set  
> the wrong expectation for the caller, if I later return a different  
> object, when my intention is only to indicate whether a query succeeds  
> or not. (You guessed it, this is motivated by ActiveRecord, but it’s  
> really a Ruby question.)

Your method’s documentation and naming the method with a question mark  
at the end will help avoid this. I would be more concerned with  
leaking internal information. That’s why people often do this in  
order to force conversion to true / false:

def is\_there\_one? whatever  
!! some\_call\_to\_lib\_that\_returns\_object\_or\_nil(whatever)  
end

If the call is expensive then I’d just return whatever is found and  
let the caller check for nil in one of the many ways.

Btw, databases typically return always a collection - albeit it might  
be empty. That depends on your mechanics in the invoked method of  
course.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![steveybaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steveybaby](https://www.ruby-forum.com/u/steveybaby)\
**Post date:** [March 30, 2010, 2:41pm UTC](https://www.ruby-forum.com/t/coercing-nil-obj-to-false-true/185991/3 "2010-03-30T14:41:43Z")

</div>

On Mar 29, 10:30 pm, Steve H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can add something like this to my codebase, but I’m wondering if  
> something similar is already built in:
> 
> def to\_boolean x  
> return !x.nil?  
> end

If you really, really need exactly true or false based on the  
truthiness of the value, just do:  
!!x

However, I would have all your methods return either a useful value or  
nil (or when appropriate, false), and just use that value in all your  
boolean expressions.

For example, imagine an array-based stack. You could write:  
def top?  
!self.last.nil?  
end  
but I would instead recommend just writing:  
def top  
self.last  
end  
and let your users do:  
if recent = my\_stack.top  
# do something with recent  
end

---

<div class="post-metadata">

**Author:** ![steveybaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steveybaby](https://www.ruby-forum.com/u/steveybaby)\
**Post date:** [March 30, 2010, 4:06pm UTC](https://www.ruby-forum.com/t/coercing-nil-obj-to-false-true/185991/4 "2010-03-30T16:06:01Z")

</div>

On Mar 30, 12:17 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > object, when my intention is only to indicate whether a query succeeds  
> > or not. (You guessed it, this is motivated by ActiveRecord, but it’s  
> > really a Ruby question.)
> 
> Your method’s documentation and naming the method with a question mark  
> at the end will help avoid this. I would be more concerned with  
> leaking internal information.

Yep.

> That’s why people often do this in  
> order to force conversion to true / false:
> 
> def is\_there\_one? whatever  
> !! some\_call\_to\_lib\_that\_returns\_object\_or\_nil(whatever)  
> end

Ok, thanks, I find that more readable than !foo.nil?.

> If the call is expensive then I’d just return whatever is found and  
> let the caller check for nil in one of the many ways.
> 
> Btw, databases typically return always a collection - albeit it might  
> be empty. That depends on your mechanics in the invoked method of  
> course.

Yep, understood, but ActiveRecord returns nil or object when you pass  
find the :first parameter. It’s useful in some situations where you  
just want to check that a record has at least one active child,  
without caring about the children themselves, such as when you are  
cleaning up “parent” objects that have been orphaned.

---

<div class="post-metadata">

**Author:** ![steveybaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steveybaby](https://www.ruby-forum.com/u/steveybaby)\
**Post date:** [March 30, 2010, 5:34pm UTC](https://www.ruby-forum.com/t/coercing-nil-obj-to-false-true/185991/5 "2010-03-30T17:34:21Z")

</div>

On Tue, Mar 30, 2010 at 3:17 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > the wrong expectation for the caller, if I later return a different  
> > !! some\_call\_to\_lib\_that\_returns\_object\_or\_nil(whatever)  
> > end

I almost never find that I need to ‘force’ a ‘true’ boolean value in  
Ruby.

I pretty much agree with Koz about this.  
[http://www.therailsway.com/2007/8/1/dangers-of-cargo-culting](http://www.therailsway.com/2007/8/1/dangers-of-cargo-culting)

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
