[Courses] [Ruby] Lesson 2: Iterators
Anne G
anne at wjh.harvard.edu
Sun Dec 4 12:24:48 EST 2005
I think I fixed the problem with the lookforX recursion,
--- the code never reached the throw command in my first try
because helper.getinfo moved to objectinfo1, objectinfo2,
and back to objectstore...
--- Even with this problem fixed, the getinfo recursion
reaches too many levels. getinfo calls getinfo through the
info objects, which calls getinfo...
def lookforX
throw :myrecursion if self.trial?
self.increaseind
end
def getinfo
if self.notendofblock? # we are in a block, store the data
@storeArr.push(self.tokeep)
self.increaseind
self.getinfo
else # we reached the end of a block, look for a new one.
catch(:myrecursion) do
while self.notendofData?
self.lookforX
end
break
end
@helper.getinfo if self.trial?
end
end
What I wanted was three independent objects which did their
bit and told each other when to get started, but in the
actual implementation, it does not work I think because the
three objects are not actually independent. Each time we
call on an object, we tie a know which we have to untie at
some point.
On Sat, 3 Dec 2005, Anne G wrote:
> 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?
>
>
>
> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses
>
More information about the Courses
mailing list