Question: How do I redesign this code to improve modularity, coupling and cohesion in Ruby My code: # Method to write a given number to a
How do I redesign this code to improve modularity, coupling and cohesion in Ruby
My code:
# Method to write a given number to a file
def write_to_file(file, number)
file.puts number
(0...number).each { |i| file.puts i }
end
# Method to read and print data from a file
def read_from_file(file)
first_line = file.gets
end
count = first_line.to_i
(0...count).each do |i|
line = file.gets
puts "Line read: #{line}"
end
# Main method
def main
file = File.new("mydata.txt", "w")
write_to_file(file, 10)
file.close
file = File.new("mydata.txt", "r")
read_from_file(file)
file.close
end
main()
The expected output:
MacBook-Pro-6:9.1 T Fix-it mmitchell\$ ruby fix_it_answer.rb Line read: 0 Line read: 1 Line read: 2 Line read: 3 Line read: 4 Line read: 5 Line read: 6 Line read: 7 Line read: 8 Line read: 9 MacBook-Pro-6:9.1 T Fix-it mmitchell\$
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
