Hi Jake,

jake kaiden wrote in post #993355:
> class Parser
>   attr_reader :output
>   def initialize(inputfile)
>   @output = [] ## this can of course be changed to what best suits your
> purposes
>   @oktypes = %W[.eng .esp .fr]
>   self.checkType(inputfile)
>   end
>
>   def checkType(file)
>     if File.exists?(file)
>   if ! @oktypes.include?(File.extname(file))
>     puts "Unrecognized File Type"
>   else
>     @oktypes.collect{|type|
>     case
>     when file.downcase.include?(type)
>       self.parseInput(file)
>       false
>     end
>     }
>   end
>     else
>   puts "File Not Found"
>     end
>   end
>
>   def parseInput(file)
>     case
>   when file.downcase.include?(".eng")
>     self.engParse(file)
>   when file.downcase.include?(".esp")
>     self.espParse(file)
>   when file.downcase.include?(".fr")
>     self.frParse(file)
>     end
>   end
>
>   def loadData(inputfile)
>     @data = []
>     file = File.open(inputfile, 'r')
>     file.collect{|line| @data << line.chomp}
>     file.close
>   end
>
> ## here's where you do whatever parsing you need to, my examples are
> dumb... but the important thing is that you end up with @output
>
>   def engParse(file)
>     self.loadData(file)
>     @data.collect{|line|
>   @output << line.reverse}
>   end
>
>   def espParse(file)
>     self.loadData(file)
>     @data.collect{|line|
>   @output << line.upcase}
>   end
>
>   def frParse(file)
>     self.loadData(file)
>     @data.collect{|line|
>   @output << line.upcase.reverse}
>   end
>
> end #class

Initially I thought about taking this approach, but frankly I don't
know how many input files I will have, then I wanted to have an
approach so that I don't need to mess with the core classes and
any core file. I wanted changes to be local to the place where they
are necessary. I mean, when I want to add another input format
all I would have to do would be to create a new class and the code
would just work.

With this approach, I would have keep on adding more and more
methods for doing the actual parsing of different formats and what
I wanted was to offload that to another class without touching the
caller code.

Oh well, I'll keep on trying. I'm sure there's some pattern for doing
just that, maybe I just didn't implement correctly :-p

-- 
Posted via http://www.ruby-forum.com/.