# Creating empty array of items - should be a quick simple one

**URL:** <https://www.ruby-forum.com/t/creating-empty-array-of-items-should-be-a-quick-simple-one/152754>\
**Category:** Rails\
**Created:** [November 7, 2008, 5:01am UTC](https://www.ruby-forum.com/t/creating-empty-array-of-items-should-be-a-quick-simple-one/152754 "2008-11-07T05:01:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [November 7, 2008, 5:01am UTC](https://www.ruby-forum.com/t/creating-empty-array-of-items-should-be-a-quick-simple-one/152754/1 "2008-11-07T05:01:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [November 7, 2008, 5:14am UTC](https://www.ruby-forum.com/t/creating-empty-array-of-items-should-be-a-quick-simple-one/152754/2 "2008-11-07T05:14:31Z")

</div>

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](http://ruby-doc.org/core/classes/Array.html#M002232)

Cheers

–  
Robert Z.  
Zapient, LLC  
Ruby on Rails Development and Consulting

[http://www.zapient.com](http://www.zapient.com)

> **[From Java To Ruby](http://www.fromjavatoruby.com)**
>
> and everything in between...

On Nov 6, 8:01 pm, Scott K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [November 7, 2008, 5:19am UTC](https://www.ruby-forum.com/t/creating-empty-array-of-items-should-be-a-quick-simple-one/152754/3 "2008-11-07T05:19:47Z")

</div>

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 Z.  
Zapient, LLC  
Ruby on Rails Development and Consulting

[http://www.zapient.com](http://www.zapient.com)

> **[From Java To Ruby](http://www.fromjavatoruby.com)**
>
> and everything in between...

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [November 7, 2008, 9:28am UTC](https://www.ruby-forum.com/t/creating-empty-array-of-items-should-be-a-quick-simple-one/152754/4 "2008-11-07T09:28:28Z")

</div>

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 K. \<

---

<div class="post-metadata">

**Author:** ![skirkpatrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@skirkpatrick](https://www.ruby-forum.com/u/skirkpatrick)\
**Post date:** [November 7, 2008, 5:27am UTC](https://www.ruby-forum.com/t/creating-empty-array-of-items-should-be-a-quick-simple-one/152754/5 "2008-11-07T05:27:13Z")

</div>

thanks for both replies! that’s exactly what i was looking for.
