Difference between revisions of "Ruby examples"
From MohidWiki
Line 1: | Line 1: | ||
+ | ==Class IsTheRootStringToTransform== | ||
+ | |||
<htm> | <htm> | ||
<pre name="code" class="ruby"> | <pre name="code" class="ruby"> |
Revision as of 13:08, 27 March 2010
Class IsTheRootStringToTransform
class IsTheRootStringToTransform attr_reader :rootfilecontent, :list, :matches attr_reader :matchexpr, :j, :outfilename, :batch def initialize (rootstring, list) @rootfilecontent, @list = rootstring, list @list = @list.to_a if !@list.is_a?(Array) @matchexpr = @list.shift @matches = @matchexpr.tr('()','').split(' ') @closings = @matchexpr.gsub(/\(.+?\)/, '*').split(/\*| /) puts @closings.join(' ') end def isTransformedByTheBlock @j = 1 @list.each do |line| yield line @j = @j + 1 end end def buildHashOfSubs(array) hash = Hash.new array.size.times { |i| hash.store(@matches[i], @closings[2*i].chomp + array[i] + @closings[2*i + 1].chomp) } return hash end def isTransformedIntoFile(filename) @outfilename = filename isTransformedByTheBlock{ |line| filecontent = String.new( @rootfilecontent ) tokens = line.split( ' ' ) subhash = buildHashOfSubs(tokens) subhash.each { |match, sub| filecontent.replace filecontent.gsub(match, sub) } mytrgfile = File.new(filename + "_" + @j.to_s.rjust(3,"0") + ".dat", "w") mytrgfile.puts filecontent mytrgfile.close puts filecontent } end def hasTheFollowingBatchFileBlock(batchfilename) @batch = String.new batchhead = <