# Nil and empty

**URL:** <https://www.ruby-forum.com/t/nil-and-empty/72435>\
**Category:** Rails\
**Created:** [October 17, 2006, 7:59am UTC](https://www.ruby-forum.com/t/nil-and-empty/72435 "2006-10-17T07:59:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [October 17, 2006, 7:59am UTC](https://www.ruby-forum.com/t/nil-and-empty/72435/1 "2006-10-17T07:59:19Z")

</div>

First, hats off to rails and the community. I’m definitely a convert.

Question: I’m constantly outputting information like so:

\<% unless item.empty? %\>

Item: \<%= item %\>

\<% end %\>

But I’m finding that depending on where item is pulled from, the item  
will  
either be empty (ie form), or null (ie db). Short of always checking  
for  
nil first then empty, what’s the easiest way to just use empty and have  
that  
stop throwing errors if it’s called on a null object?

Thanks,

Chad

---

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [October 17, 2006, 8:01am UTC](https://www.ruby-forum.com/t/nil-and-empty/72435/2 "2006-10-17T08:01:20Z")

</div>

Chad A. wrote:

> >

Try calling blank? instead of empty?

Matthew M.  
[blog.mattmargolis.net](http://blog.mattmargolis.net)

---

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [October 17, 2006, 8:03am UTC](https://www.ruby-forum.com/t/nil-and-empty/72435/3 "2006-10-17T08:03:20Z")

</div>

On 10/17/06, Chad A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

You can have a little rescue clause in there if you like.

Item: \<%= item rescue “N/A” %\>

That should stop the errors from coming. If you search around this has  
been  
discussed a number of times. Also checkout [rails.techno-weenie.net](http://rails.techno-weenie.net) I  
think  
there are also some questions there as well.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [October 17, 2006, 8:51am UTC](https://www.ruby-forum.com/t/nil-and-empty/72435/4 "2006-10-17T08:51:25Z")

</div>

Daniel N wrote:

> On 10/17/06, Chad A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Short of always checking for  
> > nil first then empty, what’s the easiest way to just use empty and have that  
> > stop throwing errors if it’s called on a null object?
> 
> You can have a little rescue clause in there if you like.
> 
> Item: \<%= item rescue “N/A” %\>
> 
> That should stop the errors from coming.

As well as a multitude of other possible errors that might arise from  
that method call. A big naked catch-all ‘rescue’ like that scares me –  
if something unexpected happens, you’ll get no notification of it, and  
it will make the bug trickier to track down.

Much better to just deal with the specific situation – in this case,  
check for nil and then check for empty, which blank?() does for you.

Chris

---

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [October 17, 2006, 8:27am UTC](https://www.ruby-forum.com/t/nil-and-empty/72435/5 "2006-10-17T08:27:23Z")

</div>

Thanks guys… Ya I searched a bit and for some reason didn’t seem to  
find a definitive answer.

Blank works perfectly for now!
