Personal tools

Difference between revisions of "Ruby examples"

From MohidWiki

Jump to: navigation, search
(Class PatternTransform)
(Class IsTheRootStringToTransform)
Line 54: Line 54:
 
     @batch = String.new
 
     @batch = String.new
 
      
 
      
batchhead = <<HEAD
+
batchhead = < <HEAD
 
@echo off
 
@echo off
 
for %%i in (#{@outfilename}_*.dat) do (
 
for %%i in (#{@outfilename}_*.dat) do (
Line 61: Line 61:
 
     yield
 
     yield
 
      
 
      
batchfoot = <<FOOT
+
batchfoot = < <FOOT
 
)
 
)
 
@echo on
 
@echo on

Revision as of 12:12, 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 = < 


Class IsTheActionToPerformWith


class IsTheActionToPerformWith
  
  attr_reader :patterncontent, :listofsubs
  attr_reader :matchline, :batch
  
  def initialize (patterncontent, listofsubs)
    @patterncontent, @listofsubs = patterncontent, listofsubs
    @listofsubs = @listofsubs.to_a if !@listofsubs.is_a?(Array)
    @matchline = @listofsubs.shift
    @matches = @matchline.tr('()','').split(' ')
    @closings = @matchline.gsub(/\(.+?\)/, '*').split(/\*| /)
    puts @closings.join(' ')
  end
  
  #private
  def thePatternIsTransformedByTheBlock
    @j = 1
    @listofsubs.each do |line|
      yield line
      @j = @j + 1
    end
  end
  
  #private
  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 usesTransformedFilesNamedAfter(filename)
    
    
    @outfilename = filename
    thePatternIsTransformedByTheBlock{ |line|    
      filecontent = String.new( @patterncontent )
      
      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
  
  #Running
  def ing
    puts "Running #{@batchfilename}..."
  end
  
  #Running
  def ning
    ing
  end

#Method 1
  def usesTheFollowingBatchfileNameAndBlock(batchfilename)
    
    @batchfilename = batchfilename
    
    @batch = String.new
    
    batchhead = <
ACTION                   : GLUES HDF5 FILES

OUTPUTFILENAME           : MOHID_WATER_01.hdf5
 
<>
..\\Extraction\\Stride_WaterProperties_1.hdf5
..\\Extraction\\Stride_WaterProperties_2.hdf5
<>

CONVERTTOHDF5ACTION

mylistofsubs = <