# Prob with each\_with\_index

**URL:** <https://www.ruby-forum.com/t/prob-with-each-with-index/80167>\
**Category:** Ruby\
**Created:** [December 21, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/prob-with-each-with-index/80167 "2006-12-21T22:47:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [December 21, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/prob-with-each-with-index/80167/1 "2006-12-21T22:47:51Z")

</div>

lo there all,

i am having a problem making each\_with\_index do what i need it to do.  
the issue is…

i need to evaluate the gpm of a flow sensor.  
we are skipping over three database entries to get the values.  
difference in value / difference in time = gpm. ok, easy enough.

but this code

@flow\_sensor.flow\_hits.each\_with\_index do |hit, i|  
if i \> 4 # start with 3rd hit in db  
time\_diff = hit[i].date\_time.to\_i -  
hit[i-3].date\_time.to\_i  
value\_diff = hit[i].value - hit[i-3].value  
min = time\_diff / 60  
val = value\_diff / min  
plots.push(val)  
times.push(hit[i].date\_time)  
end  
end

is giving me a nil value evaluating nil.date\_time

this is for a rails app by the way.

i know i have the models set up right, because other functions are  
working.  
thats why i think my structure is wrong.

thanks.

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [December 21, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/prob-with-each-with-index/80167/2 "2006-12-21T22:55:52Z")

</div>

shawn bright wrote:

> @flow\_sensor.flow\_hits.each\_with\_index do |hit, i|  
> if i \> 4 # start with 3rd hit in db  
> No, you’re starting with the 5th !!

Index are zero-based, to you need i \>= 2 to get the third one.

```
Vince
```

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [December 21, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/prob-with-each-with-index/80167/3 "2006-12-21T22:59:53Z")

</div>

On 12/21/06, Chunyun Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> min = time_diff / 60
> 
> ```
> 
> > ```
> > if i > 4 # start with 3rd hit in db
> > 
> > ```
> > 
> > is giving me a nil value evaluating nil.date\_time
> 
> cool beans, thanks gents.  
> sk

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [December 21, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/prob-with-each-with-index/80167/4 "2006-12-21T22:57:52Z")

</div>

hit passed in the block is the elment, not the array.

Try this:

@flow\_sensor.flow\_hits.each\_with\_index do |hit, i|  
if i \> 4 # start with 3rd hit in db  
time\_diff = hit.date\_time.to\_i -  
@flow\_sensor.flow\_hits[i-3].date\_time.to\_i  
value\_diff = hit.value - @flow\_sensor.flow\_hits[i-3].value  
min = time\_diff / 60  
val = value\_diff / min  
plots.push(val)  
times.push(hit.date\_time)  
end  
end
