[Courses] [Ruby] Lesson 2: Iterators
Anne G
anne at wjh.harvard.edu
Sun Dec 4 02:58:44 EST 2005
Hi Laurel,
Thank you so much for helping with my example, so I only
have to grasp the ruby points.
I understood your example with the objecthelper. When I
tried to compile it, I got some error regarding my recursion
with lookforXs.
in `notEndofData?': stack level too deep (SystemStackError)
from /Users/anne/Desktop/dudruby8d.rb:39:in lookforX'
I tried to use catch and throw, but it did not change the
error. How do I break from a recursion like mine?
Here is the method which calls lookforX with catch/throw
def getinfo
# the first time we run this method, we are in a block,
# and we store the data
if self.notendofblock?
@storeArr.push(writeline)
self.increasind
self.getinfo
# after a while we reach the end of the block
# and look for a new one
else
catch(:myrecursion) do
self.lookforX
end
end
end
And here is lookforX
def lookforX
if self.notEndofData?
# We found the keyword that starts a subject
if self.trial?
@helper.getinfo # go store some info
throw :myrecursion # break out
# or we keep looking for the keyword
else
self.lookforX
end
else #end of file, we are done
throw :myrecursion
end
end
actually I have two recursions, lookforX method calls for
self.lookforX method, and getinfo calls for self.getinfo.
So maybe I need to break out of both recursions?
Is the use of recursions like this to be avoided? my use of
catch and throw is not very elegant here, running across two
methods. Can it be used better? Is the alternative to use a
loop?
More information about the Courses
mailing list