我想加載一個文件,將它的內容分割成數組,然後讓這個類適用於內容。線路28上,這是puts show()
線類(NoMethodError)未定義的方法'each'給Student:Class
:
class Student
def initialize(name, grade)
@name = name
@grade = grade
@grade = @grade.to_i
@newgrade = @grade*1.45
end
def show()
return "#{@name} ,#{@grade} , #{@newgrade}"
end
end
# Opening the file into an array
arr = File.open("exam_results.txt", "r+")
allStudents = Array.new
for a in arr
b = a.split(",")
name = b[0]
score = b[1]
allStudents << Student.new(@name, @grade)
end
for i in Student
puts show()
end
我得到
未定義的方法 '各自' 的學生。任何關於如何進一步研究的線索?
工作!感謝您的幫助! :) – johk