# My First Almost Serious Ruby Program - Critique it

**URL:** <https://www.ruby-forum.com/t/my-first-almost-serious-ruby-program-critique-it/71809>\
**Category:** Ruby\
**Created:** [October 11, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/my-first-almost-serious-ruby-program-critique-it/71809 "2006-10-11T17:00:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![voipfc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a5b964/32.png) [@voipfc](https://www.ruby-forum.com/u/voipfc)\
**Post date:** [October 11, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/my-first-almost-serious-ruby-program-critique-it/71809/1 "2006-10-11T17:00:01Z")

</div>

This is a follow up on what I this posting

[http://groups.google.co.uk/group/ruby-talk-google/browse\_frm/thread/b58ab3c4876da743/d70309f2a864ed85?lnk=st&q=voipfc&rnum=9&hl=en#d70309f2a864ed85](http://groups.google.co.uk/group/ruby-talk-google/browse_frm/thread/b58ab3c4876da743/d70309f2a864ed85?lnk=st&q=voipfc&rnum=9&hl=en#d70309f2a864ed85)

It is based on info from here [Best Open Source Mac Software Development Software 2023](http://freshmeat.net/articles/view/447/)  
and  
[O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/onjava/blog/2006/03/recursive_directory_list_with.html)

It is a templating program that takes a source directory, reads the  
files there and outputs them to a destination directory, adjusting both  
values and file paths alike.

# Main Program

# templatefiller.rb

require ‘find’

class Template

# Construct the template object with the template and the

# replacement values. “values” can be a hash, a function,

# or a method.

def initialize(replaceStr, values, fileReplaceStr, fileValues)  
@replaceStrs = {}  
if values.kind\_of?( Hash )  
@replaceStrs[replaceStr] = values.method( :fetch )  
else  
@replaceStrs[replaceStr] = values.clone()  
end

```
@fileReplaceStrs = {}
if fileValues.kind_of?( Hash )
  @fileReplaceStrs[fileReplaceStr] = fileValues.method( :fetch )
else
  @fileReplaceStrs[fileReplaceStr] = fileValues.clone()
end

```

end

def run(inStream)  
outStream = inStream.clone()  
@replaceStrs.keys.each { |fileReplaceStr|  
outStream.gsub!( /#{fileReplaceStr}(.\*?)#{fileReplaceStr}/ ) {  
@replaceStrs[fileReplaceStr].call( $1 ).to\_s  
}  
}  
outStream  
end

def runFile(inStream)  
outStream = inStream.clone()  
@fileReplaceStrs.keys.each { |fileReplaceStr|  
outStream.gsub!( /#{fileReplaceStr}(.\*?)#{fileReplaceStr}/ ) {  
@fileReplaceStrs[fileReplaceStr].call( $1 ).to\_s  
}  
}  
outStream  
end

def to\_s()  
run();  
end  
end

if File.exists?(ARGV[0])  
inputFile = ARGV[0];  
else  
puts “File #{ARGV[0]} does not exist.\n”  
exit  
end

ini\_hash = {}  
ini\_hash[“”] = section\_hash = {} # default unnamed section

data = File.read(inputFile)

data.each do |line|  
if /^[.+]$/ =~ line # section headers are surrounded by brackets  
ini\_hash[line.chomp.gsub(/[[]]/, ‘’)] = section\_hash = {}  
else  
key,value = line.chomp.split(“=”,2)  
if value  
section\_hash[key] = value  
#puts " #{key} is #{value} "  
else  
#puts "key is #{key} nil? How about value? #{value} and  
section\_hash[#{key}] = " if key.nil?  
end  
end  
end

# example of walking through the whole thing

ini\_hash.each do |k, v|  
unless k.empty?

# v.each {|sk, sv| puts " #{sk}=#{sv}"}

```
	v.each do |sk, sv|
		unless sk.nil?

```

# 

```
		end
	end
end

```

end

$matchesTemplate = Template.new(ini\_hash[“Directories”][“escape”],  
ini\_hash[“TemplateValues”], ini\_hash[“Directories”][“outEscape”],  
ini\_hash[“PathTemplates”])

inDir = ini\_hash[“Directories”][“input\_directory”]  
outDir = ini\_hash[“Directories”][“output\_directory”]  
puts “Input Directory is - #{inDir}”  
puts “Output Directory is - #{outDir}”  
dirs = [inDir]  
excludes = [“CVS”,“classes”,“images”,“lib”,“tlds”]  
for dir in dirs  
Find.find(dir) do |path|  
if FileTest.directory?(path)  
if excludes.include?(File.basename(path))  
Find.prune # Don’t look any further into this directory.  
else  
next  
end  
else  
if FileTest.file?(path)  
if excludes.include?(File.basename(path)) == true  
next  
end  
data = File.read(path)  
outFileName = outDir + path[inDir.length, path.length -  
inDir.length]  
zFileName = $matchesTemplate.runFile(outFileName)  
f = File::open( zFileName, “w” )  
f \<\< $matchesTemplate.run(data)  
f.close()  
end  
end  
end  
end

==============

sample template settings.  
#template.ini

[Directories]  
input\_directory=.\templateSource  
output\_directory=.\templateDestination  
escape=:::  
outEscape=\_\_\_  
[TemplateValues]  
aaaa=Mary  
bbbb=Had  
cccc=a  
dddd=Little  
eeee=Lamb  
[PathTemplates]  
template=workdone
