我对 ruby 有疑问,它不会将我拥有的文件加载到指定的数组中。
class Dancer
def Initialize (couplenumber, score1, score2, score3, score4, score5, score6, score7)
@couplenumber = couplenumber
@score1 = score1
@score2 = score2
@score3 = score3
@score4 = score4
@score5 = score5
@score6 = score6
@score7 = score7
end
def show()
return "Couple Number: #{@couplenumber}. Scores: #{@score1}, #{@score2}, #{@score3}, #{@score4}, #{@score5}, #{@score6}, #{@score7}."
end
end
results = File.open("danceresult.txt", "r+")
dancescores = []
# Splitting dance scores with "," and putting into arrays.
for dancers in results
a = dancers.split(",")
couplenumber = a[0]
score1 = a[1]
score2 = a[2]
score3 = a[3]
score4 = a[4]
score5 = a[5]
score6 = a[6]
score7 = a[7]
dancescores << Dancer.new
end
dancescores.each do |dance|
puts dance.show
end
我的问题是Ruby只通过了这个:
Couple Number: . Scores: , , , , , , .
Couple Number: . Scores: , , , , , , .
Couple Number: . Scores: , , , , , , .
Couple Number: . Scores: , , , , , , .
Couple Number: . Scores: , , , , , , .
Couple Number: . Scores: , , , , , , .
我不太擅长编码,但仍在努力学习:-) 在此先感谢。