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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-23 17:19 UTC
Re: check for nil array
Hi -- On Mon, 23 Oct 2006, Sam Woodard wrote:> > 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?What you''ve got should do it. Are you sure 0-8 are not empty arrays rather than nil? David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
unknown wrote:> Are you sure 0-8 are not empty arrays > rather than nil? > > > David > > -- > David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org > 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.orgHow would I check for that? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Sam Woodard wrote:> unknown wrote: >> Are you sure 0-8 are not empty arrays >> rather than nil? >> >> >> David >> >> -- >> David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org >> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-23 17:39 UTC
Re: check for nil array
Hi -- On Mon, 23 Oct 2006, Sam Woodard wrote:> > Sam Woodard wrote: >> unknown wrote: >>> Are you sure 0-8 are not empty arrays >>> rather than nil? >> >> 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.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 | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
if you say @month.compact! ruby removes any nil elements from the array. Its great for exactly this purpose. dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> Hi -- > > On Mon, 23 Oct 2006, Sam Woodard wrote: > > >> Sam Woodard wrote: >> >>> unknown wrote: >>> >>>> Are you sure 0-8 are not empty arrays >>>> rather than nil? >>>> >>> 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. >> > > 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 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Nice, I was not aware of that. Ivor wrote:> if you say @month.compact! ruby removes any nil elements from the array. > Its great for exactly this purpose. > > dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: > >> Hi -- >> >> On Mon, 23 Oct 2006, Sam Woodard wrote: >> >> >> >>> Sam Woodard wrote: >>> >>> >>>> unknown wrote: >>>> >>>> >>>>> Are you sure 0-8 are not empty arrays >>>>> rather than nil? >>>>> >>>>> >>>> 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. >>> >>> >> 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 >> >> >> > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-25 15:46 UTC
Re: check for nil array
Hi -- On Mon, 23 Oct 2006, Ivor wrote:> dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: >> Hi -- >> >> On Mon, 23 Oct 2006, Sam Woodard wrote: >> >> >>> Sam Woodard wrote: >>> >>>> unknown wrote: >>>> >>>>> Are you sure 0-8 are not empty arrays >>>>> rather than nil? >>>>> >>>> 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. >>> >> >> 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. >> > > 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 | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---