有人要求我改进某些文件处理的模块化分解。不过,我不太确定如何进行改进。
我当时正在考虑将其放入write
和read
之类的单独函数中,然后主要调用这些函数。
def read_data_from_file(aFile)
puts aFile.gets
end
def write_data_to_file(aFile)
aFile.puts('5')
end
def main
aFile = File.new("mydata.txt", "w") # open for writing
if aFile # if nil this test will be false
write_data_to_file(aFile)
aFile.close
else
puts "Unable to open file to write!"
end
aFile = File.new("mydata.txt", "r") # open for reading
if aFile # if nil this test will be false
read_data_from_file(aFile)
aFile.close
else
puts "Unable to open file to read!"
end
end