# Nil.each error

**URL:** <https://www.ruby-forum.com/t/nil-each-error/108463>\
**Category:** Rails\
**Created:** [July 3, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/nil-each-error/108463 "2007-07-03T20:52:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [July 3, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/nil-each-error/108463/1 "2007-07-03T20:52:06Z")

</div>

When I run the following code in the console I get the following error:

s\_date = Date.new(2007,6,3)  
e\_date = Date.new(2007,7,3)

due\_dates = DueDate.find(:all, :conditions =\> [‘stamp \>= ? and stamp \<=  
?’,s\_date.to\_s(:db),e\_date.to\_s(:db)], :order =\> ‘stamp desc’)

sales\_orders = []

due\_dates.each { |dd| sales\_orders \<\< dd.so\_number }

@histories = History.find\_by\_sales\_orders(sales\_orders)

@histories.compact!.each { |history|  
@total += 1  
if history.sales\_order\_has\_ec?  
if history.ec\_date \>  
DueDate.find\_by\_sales\_order(history.so\_number).stamp  
@late += 1  
elsif history.ec\_date \<  
DueDate.find\_by\_sales\_order(history.so\_number).stamp  
@early += 1  
else  
@on\_time += 1  
end  
else  
@incomplete += 1  
end  
}

NoMethodError: 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.+  
from (irb):20  
from (irb):19:in `each’  
from (irb):19

What am I doing wrong here? I checked all the arrays used here, and  
they all have values; none of them are empty.

Thanks!

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [July 3, 2007, 8:58pm UTC](https://www.ruby-forum.com/t/nil-each-error/108463/2 "2007-07-03T20:58:15Z")

</div>

> due\_dates.each { |dd| sales\_orders \<\< dd.so\_number }
> 
> @histories = History.find\_by\_sales\_orders(sales\_orders)
> 
> @histories.compact!.each { |history|

Get rid of the ‘!’.

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [July 3, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/nil-each-error/108463/3 "2007-07-03T21:01:35Z")

</div>

> Get rid of the ‘!’.

Ok, that worked! But, why did it work? I don’t understand why the !  
makes a difference…

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [July 3, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/nil-each-error/108463/4 "2007-07-03T21:27:18Z")

</div>

Hi –

On Tue, 3 Jul 2007, Jason F. wrote:

> > Get rid of the ‘!’.
> 
> Ok, that worked! But, why did it work? I don’t understand why the !  
> makes a difference…

Array#compact! is one of a number of methods that change their  
receiver but return nil if there’s no change. So:

[1,2,3].compact! # nil

Remember that the ! means “dangerous” – expect the unexpected 🙂

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [July 3, 2007, 9:29pm UTC](https://www.ruby-forum.com/t/nil-each-error/108463/5 "2007-07-03T21:29:23Z")

</div>

Makes sense. I know I’ve read that before, but obviously forgot it…

Thanks!
