Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > what's wrong in my loop ?

Reply
Thread Tools

what's wrong in my loop ?

 
 
Josselin
Guest
Posts: n/a
 
      10-02-2006
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

 
Reply With Quote
 
 
 
 
Tom Armitage
Guest
Posts: n/a
 
      10-02-2006
I think you might find it's the "i++".

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

t.

On 02/10/06, Josselin <(E-Mail Removed)> 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
>
>
>


 
Reply With Quote
 
 
 
 
Stephane Elie
Guest
Posts: n/a
 
      10-02-2006
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/.

 
Reply With Quote
 
Josselin
Guest
Posts: n/a
 
      10-02-2006
On 2006-10-02 14:54:58 +0200, Stephane Elie <(E-Mail Removed)> 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

 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      10-02-2006
On 10/2/06, Josselin <(E-Mail Removed)> wrote:
> On 2006-10-02 14:54:58 +0200, Stephane Elie <(E-Mail Removed)> 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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Getting a loop to activate a loop above it Byte Python 4 03-24-2006 03:04 AM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM



Advertisments