1
\$\begingroup\$

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

# reads in each line.
def read_data_from_file(aFile)
  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
    read_data_from_file(aFile)
    aFile.close
  else
    puts "Unable to open file to read!"
  end
end
```
\$\endgroup\$

closed as unclear what you're asking by yuri, Mathias Ettinger, Toby Speight, pacmaninbw, esote May 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.

  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – greybeard May 15 at 6:22
  • \$\begingroup\$ hmm, I would move both read and write blocks in main to their own methods. \$\endgroup\$ – Christian Jun 9 at 0:29

Browse other questions tagged or ask your own question.