what's wrong in my loop ?

Discussion in 'Ruby' started by Josselin, Oct 2, 2006.

  1. Josselin

    Josselin Guest

    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
     
    Josselin, Oct 2, 2006
    #1
    1. Advertising

  2. Josselin

    Tom Armitage Guest

    I think you might find it's the "i++".

    Try "i.next" or "i += 1"

    t.

    On 02/10/06, Josselin <> wrote:
    > 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
    >
    >
    >
     
    Tom Armitage, Oct 2, 2006
    #2
    1. Advertising

  3. 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


    --
    Posted via http://www.ruby-forum.com/.
     
    Stephane Elie, Oct 2, 2006
    #3
  4. Josselin

    Josselin Guest

    On 2006-10-02 14:54:58 +0200, Stephane Elie <> said:

    > 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


    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
     
    Josselin, Oct 2, 2006
    #4
  5. On 10/2/06, Josselin <> wrote:
    > On 2006-10-02 14:54:58 +0200, Stephane Elie <> said:
    >
    > > 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

    >
    > 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/
     
    Rick DeNatale, Oct 2, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew
    Replies:
    7
    Views:
    675
    Priscilla Walmsley
    Jan 7, 2005
  2. Peter Olcott

    What is wrong with the "for" loop?

    Peter Olcott, Jul 16, 2003, in forum: C++
    Replies:
    3
    Views:
    329
    Andrew Koenig
    Jul 16, 2003
  3. Jeff
    Replies:
    3
    Views:
    568
    Karsten Baumgarten
    Feb 5, 2005
  4. Replies:
    7
    Views:
    382
    涵曦
    Oct 18, 2012
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page