Question
Ruby Code- # takes a number and writes that number to a file then on each line # increments from zero to the number passed
Ruby Code-
# 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
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
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
Q1)
Code must follow the Ruby coding convention used in the unit (layout, and use of case).
You are storing and working with multiple values in an array.
The code must run and you must capture a screenshot that shows it working in accordance with the requirements as described here.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started