From: Paul Smith Date: 2009-09-23T19:23:52+09:00 Subject: Re: Mini Parser On Wed, Sep 23, 2009 at 10:55 AM, Karthikeyan Balasubramanian wrote: > Hi all, > >  I have a requirement.  I have a to read some commands from file and > output as another file.  For example if the input file is like below: > > 6 > WRITE  Today > WRITE  is > UNDO  1 > WRITE  my > UNDO  2 > WRITE birthday > > Output file will be like this. > > Today is birthday > > WRITE basically write's string after the command and UNDO will undo N > number of line preceding it.  Complexity here is last UNDO has to UNDO > the previous undo's work. > > So far I have been able to only come up with the below code: > > begin >  file = File.new("abc.txt", "r") >  while (line = file.gets) >    line.grep(/WRITE/) { |s| >      puts s >    } >  end >  file.close > > rescue => err >  puts "Exception: #{err}" >  err > end > > Any help here? > -- > Posted via http://www.ruby-forum.com/. > > If you want undoable commands you need to keep a command history. I'd check out the Command design pattern if I were you. -- Paul Smith http://www.nomadicfun.co.uk paul@pollyandpaul.co.uk