Home How do i read lines in txt and use them in a method for ruby, exactly as the text is?

# How do i read lines in txt and use them in a method for ruby, exactly as the text is?

 class Test def self.take_test( question, options, answer ) puts question options.each_with_index { |option, idx| puts "#{ idx + 1 }: #{ option}" } print "Answer: " reply = gets.to_i if answer == reply puts "Correct!" else puts "Wrong. The answer is: " + answer.to_s end end end file = File.open("Matematik.txt", "r")  This is what i tried to do:  IO.foreach("Matematik.txt") { |line| Test.take_test(line) }  This is how the questions are set up in the file: 'What is 2+2?', [ '2', '3', '4', '5', ], 4 'What is 3+3?', [ '3', '6', '9', ], 6 I get the error: take_test wrong number of arguments given (given 1, expected 3) (ArgumentError) It seems like it reads the line like 1 argument. Is there a way to read the lines exactly as i stands, and input it like this?: #Test.take_test('What is 2+2?', [ '2', '3', '4', '5', ], 4) 
 In theory, yes, with eval("Test.take_test(#{line})"). However, eval is evil, and to be avoided if at all possible. It would be much easier if you changed your file format so you can deconstruct it easily. (It is not impossible with your format, it's just you have a lot of unnecessary work, when compared to a simpler format.) For example, given lines formatted like CSV: "What is 2+2?",2,3,4,5,4  it is very easy to do the following: require 'csv' Question = Struct.new(:text, :options, :answer) questions = CSV.read("Mathematik.csv").map { |text, *options, answer| Question.new(text, options, answer.to_i) } questions[0].text # => "What is 3+3?" questions[0].options # => ["2", "3", "4", "5"] questions[0].answer # => 4