Difference between revisions of "Ruby examples"
From MohidWiki
m (1 revision) |
|||
| Line 1: | Line 1: | ||
| − | <code> | + | <htm> |
| + | <pre name="code" class="ruby"> | ||
class IsTheRootStringToTransform | class IsTheRootStringToTransform | ||
| Line 70: | Line 71: | ||
end | end | ||
| − | </ | + | </pre> |
| + | </htm> | ||
[[Category:Programming]] | [[Category:Programming]] | ||
[[Category:Ruby]] | [[Category:Ruby]] | ||
[[Category:Windows]] | [[Category:Windows]] | ||
Revision as of 11:50, 27 March 2010
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 = <