From: Eric Mahurin Date: 2005-05-21T23:04:26+09:00 Subject: Re: [ANN] cursor-0.5 --- Simon Strandgaard wrote: > On 5/20/05, Eric Mahurin wrote: > [snip] > > Here are the current cursor classes I have: > [snip] > > Please add a few example files to your package on how to > use each of these classes. Sorry, I'll get to that later. More importantly, I need examples for the methods. Here is a example that might be used in a parser or lexer (my target apps) to give you an idea of what it can do: require 'stringio' # you'll really want something with begin and if io = StringIO.new("beginififbeginbeginif") c = Cursor::IO.new(io) # this would get rid of the seeks - but it has bugs #c = Cursor::Buffered.new(Cursor::IO.new(io)) loop do # see if next 5 chars are "begin" and go back if not if c.position?{c.get(5)=="begin"} puts('process "begin" here') # see if next 2 chars are "if" and go back if not elsif c.position?{c.get(2)=="if"} puts('process "if" here') else break end end raise if !c.eof? > Your Cursor#get and Cursor#put methods looks a bit longish. I know, they are not pretty - these methods are quite overloaded. The primary advantage of doing this is that many of methods that use these can get all of this extra functionality for free. Take [], []=, each, copy_from, copy_to, etc. for example. If I didn't do this, the number of methods would blow up to get the same functionality. This is similar to the overloading done in String#[] for example. > Having + and - as public methods is maybe dangerous, you > risk not closing the instances after usage. The same can be said of succ (used in a Range) and pred. All of these methods create "child" cursors that the parent knows about (with object_id). The children use their parent to get their work done. When the parent closes, it closes all of the children. The parent also defines a finalizer for each child to remove it from its children list. closing a child is not really a necessary thing to do - the finalizer will clean things up. Discover Yahoo! Have fun online with music videos, cool games, IM and more. Check it out! http://discover.yahoo.com/online.html