[Courses] [Ruby] Lesson 2: Iterators

Anne G anne at wjh.harvard.edu
Sat Dec 10 06:47:58 EST 2005


HI

> Without seeing all of your code, it's hard to tell what
> exactly the problem is, or whether there would be a
> better way to implement it without recursion.  Would you
> be willing to post the whole thing to the list?

Yes of course. But at this point my question is more
conceptual:

I changed the code so that within each block of data, I use
a loop. This way, I only run getdata, getinfo1 and get info2
once per subject, and so the maximum level of recursion is
the number of subjects, around 200, a number which ruby can
handle, and there is no error.

> Each recursive method call causes a little bit of
> overhead (it needs to keep track of every method
> currently running), and this can add up over time.

I assume that OOP can handle open ended, real time
situations. So a working design would need to go back
to a neutral point from time to time to clear out the
methods running.

In my case,
getdata finds a header
getdata  calls getinfo1,
getinfo1 calls getinfo2
getinfo2 calls getdata
getdata stores the subjectdata.

At this point there is a clear breaking point, and I ought
to go back to neutral. Is there a ruby command which I could
use to say go back to top level, forget about tracking down
all those methods?

anne
On Fri, 9 Dec 2005, Laurel Fan wrote:

> On 12/9/05, Anne G <anne at wjh.harvard.edu> wrote:
> > This worked very well, getdata calls getinfo1 which calls
> > getinfo2 which calls getdata ad infinitum. The problem I
> > encounted is that ad infinitum is not supported by my
> > implementation of Ruby, I got an error message, too many
> > levels (each repeat call on getdata is a level).
> >
> > Of course I can find ways to write the code more efficiently
> > to avoid the limit, but by calling a method for each line of
> > code, I encountered a limit, and the fact that the limit
> > exists is what bothers me. That means I have to make sure
> > the whole program is done is so many calls.  This is not a
> > stable timeless implementation. It could not run on live
> > data that kept coming and coming.
>
> Now I think I understand a bit more of what the problem is.
>
> Recursion (methods calling themselves) might not be the best solution
> for data that keeps coming continuously.  Each recursive method call
> causes a little bit of overhead (it needs to keep track of every
> method currently running), and this can add up over time.
>
> It's also possible that there's a bug in the code causing an infinite
> recursion loop.
>
> Without seeing all of your code, it's hard to tell what exactly the
> problem is, or whether there would be a better way to implement it
> without recursion.  Would you be willing to post the whole thing to
> the list?
>
> --
> Laurel Fan
> http://dreadnought.gorgorg.org
>



More information about the Courses mailing list