Check for nil array

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

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 | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

unknown wrote:

Are you sure 0-8 are not empty arrays
rather than nil?

David


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

How would I check for that?

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 | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Sam W. wrote:

unknown wrote:

Are you sure 0-8 are not empty arrays
rather than nil?

David


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] 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

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

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

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 | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Nice, I was not aware of that.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs