# Check for nil array

**URL:** <https://www.ruby-forum.com/t/check-for-nil-array/73308>\
**Category:** Rails\
**Created:** [October 23, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/check-for-nil-array/73308 "2006-10-23T19:14:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 23, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/check-for-nil-array/73308/1 "2006-10-23T19:14:38Z")

</div>

I have the following code:

\<% for month in @months %\>  
\<%= render(:partial =\> “month” , :object =\> month) if month %\>

Calendar year, 2006 is only partially complete–the first record is  
October 16, 2006. Therefore, I only want rails to render the partial if  
month is not nil. @months[0]-@months[8] are nil. However the code  
above does not work.

What is the correct code to check and see if the array, month is not a  
nil object before rendering the partial?

Thanks,  
Sam

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 23, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/check-for-nil-array/73308/2 "2006-10-23T19:24:07Z")

</div>

Hi –

On Mon, 23 Oct 2006, Sam W. wrote:

> What is the correct code to check and see if the array, month is not a  
> nil object before rendering the partial?

What you’ve got should do it. Are you sure 0-8 are not empty arrays  
rather than nil?

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 23, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/check-for-nil-array/73308/3 "2006-10-23T19:31:19Z")

</div>

unknown wrote:

> Are you sure 0-8 are not empty arrays  
> rather than nil?
> 
> David
> 
> –  
> David A. Black | removed\_email\_address@domain.invalid  
> Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
> DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

How would I check for that?

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 23, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/check-for-nil-array/73308/4 "2006-10-23T19:41:39Z")

</div>

Hi –

On Mon, 23 Oct 2006, Sam W. wrote:

> months 0-8 in the @months corresponding to 2006.  
> You could do:

\<% for month in @months %\>  
\<% next unless month[0] %\> # test for non-nil value

or some similar test. Or, you could do this in your controller:

@months.delete\_if {|m| m[0].nil? }

or some other filtering operation, so that your @months array only  
contains the sub-arrays you want.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 23, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/check-for-nil-array/73308/5 "2006-10-23T19:33:58Z")

</div>

Sam W. wrote:

> unknown wrote:
> 
> > Are you sure 0-8 are not empty arrays  
> > rather than nil?
> > 
> > David
> > 
> > –  
> > David A. Black | removed\_email\_address@domain.invalid  
> > Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
> > DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> > [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> > [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
> 
> How would I check for that?

@months is an array of arrays. Month is that array containing Day  
objects. Each Day object is nil in the month arrays in @month for  
months 0-8 in the @months corresponding to 2006.

Sam

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 23, 2006, 9:04pm UTC](https://www.ruby-forum.com/t/check-for-nil-array/73308/6 "2006-10-23T21:04:33Z")

</div>

if you say @month.compact! ruby removes any nil elements from the array.  
Its great for exactly this purpose.

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 26, 2006, 7:37am UTC](https://www.ruby-forum.com/t/check-for-nil-array/73308/7 "2006-10-26T07:37:34Z")

</div>

Perhaps you could look at your code that generates and manipulates the  
array and arrange for any unused element to always be nil. Or, you  
could turn it into an object which has its own ‘hascontent?’  
flag/method?

Tonypm

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 25, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/check-for-nil-array/73308/8 "2006-10-25T17:48:01Z")

</div>

Hi –

On Mon, 23 Oct 2006, Ivor wrote:

> > > > > Are you sure 0-8 are not empty arrays
> 
> if you say @month.compact! ruby removes any nil elements from the array.  
> Its great for exactly this purpose.

Since the “if month” test wasn’t working (i.e., it was always  
returning true), I’m assuming that the elements Sam wants to skip are  
not nil. I think they’re empty arrays, which compact! will not  
remove.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 23, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/check-for-nil-array/73308/9 "2006-10-23T21:45:47Z")

</div>

Nice, I was not aware of that.
