ruby – move as many lines of # code out of the main line as possible


How can I move the lines of code out of the main if possible?
without messing up the program. Please, ask me if you have not underestimated the question. Thank you

def main
  a_file = File.new("mydata.txt", "w") # open for writing
  if a_file  # if nil this test will be false
    write_data_to_file(a_file)
    a_file.close
  else
    puts "Unable to open file to write!"
  end

  a_file = File.new("mydata.txt", "r") # open for reading
  if a_file  # if nil this test will be false
    read_data_from_file(a_file)
    a_file.close
  else
    puts "Unable to open file to read!"
  end
end

main