Question: Help me fix my Fix It code in Ruby? I need to redesign the code to improve modularity, coupling and cohesion. My code is below:

Help me fix my Fix It code in Ruby?

I need to redesign the code to improve modularity, coupling and cohesion. My code is below:

# takes a number and writes that number to a file then on each line

# increments from zero to the number passed

def write(aFile, number)

# You might need to fix this next line:

aFile.puts("number")

index = 0

while (index < number)

aFile.puts(number.to_s)

index += 1

end

end

# Read the data from the file and print out each line

def read(aFile)

# Defensive programming:

count = aFile.gets

if (is_numeric(count))

count = count.to_i

else

count = 0

puts "Error: first line of file is not a number"

end

index = 0

while (count < index)

line = aFile.gets

puts "Line read: " + line

end

end

# Write data to a file then read it in and print it out

def main

aFile = File.new("mydata.txt", "w") # open for writing

write(aFile, 10)

read(aFile)

aFile.close

end

# returns true if a string contains only digits

def is_numeric?(obj)

if /[^0-9]/.match(obj) == nil

true

end

false

end

main

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!