# What's wrong in my loop?

**URL:** <https://www.ruby-forum.com/t/whats-wrong-in-my-loop/70734>\
**Category:** Ruby\
**Created:** [October 2, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/whats-wrong-in-my-loop/70734 "2006-10-02T14:20:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 2, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/whats-wrong-in-my-loop/70734/1 "2006-10-02T14:20:46Z")

</div>

I wrote the following loop some records ,

init an array db  
get a record , loop on all records indexing by i  
while record.start\_at \<= record.end\_at  
put in the db array [record.start\_at, i]  
end while  
loop on records  
end

but it seems that’s Ruby doesn’t like it … what’s wrong ? is theer any  
way to write it better ?

```
  db = []
 record.each do |b|
    aDate = b.start_at
    i = 0
    while aDate <= b.end_at
      db << [aDate, i]
      i++
      aDate= aDate + i
    end
  end

```

jossss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 2, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/whats-wrong-in-my-loop/70734/2 "2006-10-02T14:26:46Z")

</div>

I think you might find it’s the “i++”.

Try “i.next” or “i += 1”

t.

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 2, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/whats-wrong-in-my-loop/70734/3 "2006-10-02T14:54:54Z")

</div>

Hi Josselin,

Tom is right about the “i++”.

Also, it doesn’t look like your pseudo-code matches your ruby code  
functionality.

I’ll give it a shot as what you may want to do, just a guess…

db = []  
record.each do |b|  
(b.end\_at - b.start\_at).times { |i| db \<\< [b.start\_at+i, i] }  
end

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 2, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/whats-wrong-in-my-loop/70734/4 "2006-10-02T19:05:31Z")

</div>

On 2006-10-02 14:54:58 +0200, Stephane E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> record.each do |b|  
> (b.end\_at - b.start\_at).times { |i| db \<\< [b.start\_at+i, i] }  
> end

thansk Tom & Steph  
newbie I am mixin any languages into it…  
I actually need to remember first that Ruby is OO, so i.next makes more  
sense compared to i++

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [October 3, 2006, 12:04am UTC](https://www.ruby-forum.com/t/whats-wrong-in-my-loop/70734/5 "2006-10-03T00:04:16Z")

</div>

On 10/2/06, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > db = []  
> > record.each do |b|  
> > (b.end\_at - b.start\_at).times { |i| db \<\< [b.start\_at+i, i] }  
> > end
> 
> thansk Tom & Steph  
> newbie I am mixin any languages into it…  
> I actually need to remember first that Ruby is OO, so i.next makes more  
> sense compared to i++

The expression i++ isn’t valid ruby.

Keep in mind that

i.next

all by itself has no effect on i:

i = 2  
p i.next =\> 3  
p i =\> 2

Don’t confuse the variable i with the object it contains. The  
expression i++ simply asks the object 2 for it’s successor leaving it  
unchanged.

i += 1

which is the same as:

i = i + 1

computes the value of i + 1 and assigns the result to the variable i.

you could also use

i = i.next

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
