Hi Group,
Forgive me to put 3 seemingly irrelevant words in the subject line. But
they are the purposes of this post.
This is basically my first Ruby class, which is to remove all empty
lines and blank times from a txt file. a simple test case is also
included.
I tested the code and it works fine. But I did not have much idea about
if the code style is good, or if there is some potential issues. Can
someone pls do a quick review and kindly provide some feedback?
I want to know -
-
Is there any potential issue in the code? did I miss some boundary
check? -
Is the code style good in general?
e.g, name convention, indent, etc. -
what can i do if i want to make it more professional?
e.g., better choice of functions, error-handling, object-oriented,
test-driven, etc.
I am open to any feedback - as far as they help make better Ruby
programing.
==================================================================
a copy is included as below
require ‘test/unit’
this class is to remove all the empty and blank lines.
Usage: ruby Blanker.rb input.txt output.txt
class BlankLineRemover
def initialize()
puts “BlankLineRemover initialized.”
end
def generate(input_filename, output_filename)
reader = File.read(input_filename)
f = File.new(output_filename, "w")
f.puts remove(reader)
f.close
end
def remove(stringToremove)
regEx = /^[\s]*$\n/
stringToReturn = stringToremove.gsub(regEx, '')
stringToReturn.strip!
if stringToReturn.length >= 1
return stringToReturn
else
return ""
end
end
end
class TestCleaner < Test::Unit::TestCase
def test_basic
sInput1 = "line1\n\t\nline4\n\tline5\n"
sExpectedOutput1 = "line1\nline4\n\tline5"
sInput2=""
sExpectedOutput2 = ""
sInput3="\n\t\t\n"
sExpectedOutput3 = ""
testCleaner = BlankLineRemover.new
assert_equal(sExpectedOutput1, testCleaner.remove(sInput1))
assert_equal(sExpectedOutput2, testCleaner.remove(sInput2))
assert_equal(sExpectedOutput3, testCleaner.remove(sInput3))
end
end
unless ARGV.length == 2
puts “Usage: ruby Blanker.rb input.txt output.txt\n”
exit
end
simpleRemover = BlankLineRemover.new
simpleRemover.generate(ARGV[0],ARGV[1])
==================================================================