From: jake kaiden Date: 2011-04-18T02:01:47+09:00 Subject: Re: Making a simple parser hi felipe, well, i don't know how many possible input types you have, but if they're not too very many, you could try a very different approach: ## for this test i created three files, "input.eng," "input.esp," and "input.fr," each with a few lines of random text... 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 test = Parser.new("input.esp") puts test.output this may be WAY too simple for what you're trying to do, but hey, maybe not! ;) - j -- Posted via http://www.ruby-forum.com/.