i created a method that allows you to write a question in one line and it's answer in the immediately next line, so that file has only questions in the odd lines and answers to those questions in the even lines, i want to add the odd lines (questions) as strings in an array and the even ones (answers) also as strings but in a different array, so the answer of the question that is located at @questions in the first place, is located at @answers in the first place.
This is my code:
def initialize
@file = File.new("questionary.out", "a")
@questions = []
@answers = []
end
def append_to_file
puts
puts "PLEASE TYPE THE QUESTION THAT YOU WANT TO ADD"
puts
append_question = gets
@file << append_question
puts
puts "PLEASE TYPE IT'S ANSWER"
puts
append_answer = gets
@file << append_answer
@file.close
#INSERT INTO THE ARRAYS
i = 0
File.open("questionary.out") do |line|
i = i+1
if i % 2 == 0
@answers << line.to_s
else
@questions << line.to_s
end
end
end
For some reason, when i print my arrays, @questions shows strange characters, which i think are an object of the class File, while @answers stays empty.
Thanks a million for reading this.