Scott Kulik
2008-Nov-07 04:01 UTC
creating empty array of items - should be a quick simple one
here is some of the code: @items_all = Item.find(:all) for item in @items_all do if not FileTest.exist?(item.image_name) @items_missing_images += item end end You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.+ at @items_missing_images += item so...how can i declare an empty array of Items? i tried declaring @items_missing_images = [] but item is not of type array so this won''t work. -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Chas Lemley
2008-Nov-07 04:06 UTC
Re: creating empty array of items - should be a quick simple one
I''m new to this but could you do this: above this put @items_missing_images = Array.new and then in your if statement do this @items_missing_images << item Chas On Thu, Nov 6, 2008 at 11:01 PM, Scott Kulik < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > here is some of the code: > > @items_all = Item.find(:all) > > for item in @items_all do > if not FileTest.exist?(item.image_name) > @items_missing_images += item > end > end > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occurred while evaluating nil.+ > > at @items_missing_images += item > > so...how can i declare an empty array of Items? > > i tried declaring @items_missing_images = [] > > but item is not of type array so this won''t work. > -- > 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?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Zotter
2008-Nov-07 04:14 UTC
Re: creating empty array of items - should be a quick simple one
Scott, You need to declare the empty array first. @items_missing_images = [] But do not use the ''@items_missing_images += item '' Use @items_missing_images.push(item) or @items_missing_images << item The += only works if you are adding an array to an array. http://ruby-doc.org/core/classes/Array.html#M002232 Cheers -- Robert Zotter Zapient, LLC Ruby on Rails Development and Consulting http://www.zapient.com http://www.fromjavatoruby.com On Nov 6, 8:01 pm, Scott Kulik <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> here is some of the code: > > @items_all = Item.find(:all) > > for item in @items_all do > if not FileTest.exist?(item.image_name) > @items_missing_images += item > end > end > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occurred while evaluating nil.+ > > at @items_missing_images += item > > so...how can i declare an empty array of Items? > > i tried declaring @items_missing_images = [] > > but item is not of type array so this won''t work. > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Zotter
2008-Nov-07 04:18 UTC
Re: creating empty array of items - should be a quick simple one
I probably should mention the best way to do what you are trying to accomplish would be: @items_missing_images = @items_all.reject { |item| FileTest.exist? (item.image_name) } -- Robert Zotter Zapient, LLC Ruby on Rails Development and Consulting http://www.zapient.com http://www.fromjavatoruby.com On Nov 6, 8:14 pm, Robert Zotter <rzot...-pPleKHUOxhtBDgjK7y7TUQ@public.gmane.org> wrote:> Scott, > > You need to declare the empty array first. > @items_missing_images = [] > > But do not use the ''@items_missing_images += item '' > Use @items_missing_images.push(item) > or @items_missing_images << item > > The += only works if you are adding an array to an array. > > http://ruby-doc.org/core/classes/Array.html#M002232 > > Cheers > > -- > Robert Zotter > Zapient, LLC > Ruby on Rails Development and Consulting > > http://www.zapient.comhttp://www.fromjavatoruby.com > > On Nov 6, 8:01 pm, Scott Kulik <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > here is some of the code: > > > @items_all = Item.find(:all) > > > for item in @items_all do > > if not FileTest.exist?(item.image_name) > > @items_missing_images += item > > end > > end > > > You have a nil object when you didn''t expect it! > > You might have expected an instance of Array. > > The error occurred while evaluating nil.+ > > > at @items_missing_images += item > > > so...how can i declare an empty array of Items? > > > i tried declaring @items_missing_images = [] > > > but item is not of type array so this won''t work. > > -- > > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Scott Kulik
2008-Nov-07 04:27 UTC
Re: creating empty array of items - should be a quick simple one
thanks for both replies! that''s exactly what i was looking for. -- 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?hl=en -~----------~----~----~----~------~----~------~--~---