# Ruby exercise in writing and reading a file [closed]

I've been asked to improve the modular decomposition of the main by moving as many lines of code out of main as possible, but i have no idea how i should do this

# writes the number of lines then each line as a string.
def write_data_to_file(aFile)
aFile.puts('5')
end

puts aFile.gets
end

# writes data to a file then reads it in and prints each line as it reads
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
aFile.close
else
puts "Unable to open file to read!"
end
end
$$$$


## closed as unclear what you're asking by yuri, Mathias Ettinger, Toby Speight, pacmaninbw, esoteMay 16 at 4:22

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Welcome to Code Review. Try putting a newline after the closing . The title should reflect what the code is to achieve, see How do I ask a Good Question? – greybeard May 15 at 6:22
• hmm, I would move both read and write blocks in main to their own methods. – Christian Jun 9 at 0:29