Here is my story about my first ruby script ever, actually it was the first time I writing coded in 2 years time. So please be gentle to my awful code
Purpose was to help PO to get a complete “read me” file over every patch the “Team” created.
So why not try something new while solving the problem? I started out by “just do it” with some help of guessing and google, Here is my first version of the code:
Round 1
def createAndWriteToFile(file, text)
myFile = File.new(file, "w")
myFile.write(text)
end
def getPatchID(path)
myRes = path.scan(/.*Patch_(.*)\//i)
return myRes.to_s
end
def getOutputHeader(x)
outString = "\n ----------------------------------------------------------\n"
outString += "Patch: " + getPatchID(x) + "\n"
outString += "\n ----------------------------------------------------------\n"
return outString
end
def getFileContent(x)
fileContent = ""
file = File.new(x, "r")
while (line = file.gets)
fileContent+= line
end
file.close
return fileContent
end
def buildFileOutput(x)
fileContent = getOutputHeader(x)
fileContent += getFileContent(x)
return fileContent
end
def getAllFilesOutput(files)
filesContent = ""
files.each {|x|
filesContent += buildFileOutput(x)
}
return filesContent
end
#Run
createAndWriteToFile("AllReadme.txt",getAllFilesOutput(Dir["./*/README.txt"]))
Round 2
After asking Niclas Nilsson to review my code (on paper) I learned some new things.
- “returns” are optional.
- “()” after method name is optional.
- In Ruby “we” don’t use camel case
- There is easier way to open and write to a file.
- And last but not the smallest lesson the power of “.map”
So here is my new version of the code.
def create_and_write_to_file file, text
File.new(file, "w").write(text)
end
def get_patch_id path
path.scan(/.*Patch_(.*)\//i).to_s
end
def get_output_header x
out_string = "\n ----------------------------------------------------------\n"
out_string += "Patch: " + get_patch_id(x) + "\n"
out_string += "\n ----------------------------------------------------------\n"
end
def build_file_output x
get_output_header(x) + File.read(x)
end
def get_all_files_output files
files.map{ |file|build_file_output(file) }
end
create_and_write_to_file("AllReadme.txt",get_all_files_output(Dir["./*/README.txt"]))
Round 3
Okay hang on now it was Anders Janmyr turn to take a look at it.
This is how he would like to do the code.
def get_patch_id path
path.scan(/.*Patch_(.*)\//i).to_s
end
def get_output_header path
out_string = "\n ----------------------------------------------------------\n"
out_string += "Patch: " + get_patch_id(path) + "\n"
out_string += "\n ----------------------------------------------------------\n"
end
File.open("AllReadme.txt", "w") do |output_file|
readme_paths = Dir["./*/README.txt"]
readme_paths.each do |readme_path|
output_file.write get_output_header(readme_path)
output_file.write File.read(readme_path)
end
end
Round 4
Okay I get it, in ruby we don’t write too many lines or what?
Here is my final version.
def get_output_header path
out_string = "\n ----------------------------------------------------------\n"
out_string += "Patch: " + path.scan(/.*Patch_(.*)\//i).to_s + "\n"
out_string += "\n ----------------------------------------------------------\n"
end
File.open("AllReadme.txt", "w") do |output_file|
readme_paths = Dir["./*/README.txt"]
readme_paths.each do |readme_path|
output_file.write get_output_header(readme_path) + File.read(readme_path)
end
end
Round 5
Now its your turn to give me feedback. Which version would you prefer and why? Would you write the code differently?
Please feel free to comment.
Thanks to Niclas Nilsson and Anders Janmyr for taking the time to help me in my learning process.
My next project in ruby is ongoing and will be a experiment with twitter and the home made candy machine called “SlickStreamer” developed by me and Marcus Olsson . I will soon post more information about this project.