Ruby-不会将文件加载到数组中 [英] Ruby - won't load file into arrays
问题描述
我对ruby有问题,它不会将我拥有的文件加载到指定的数组中。
I have a problem with ruby, it won't load the file i have into the designated arrays.
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仅通过了:
My problem is that Ruby only passes this:
Couple Number: . Scores: , , , , , , .
Couple Number: . Scores: , , , , , , .
Couple Number: . Scores: , , , , , , .
Couple Number: . Scores: , , , , , , .
Couple Number: . Scores: , , , , , , .
Couple Number: . Scores: , , , , , , .
我不太擅长编码,仍在尝试学习:-)谢谢。
I'm not very good at coding and still trying to learn :-) Thanks in advance.
推荐答案
此处的一些问题:
-
该方法称为
初始化
,而不是初始化
—大写很重要。
The method is called
initialize
, notInitialize
— capitalization is important.
您在不同的地方有一堆名称相同的变量,您似乎认为它们将是相同的变量,但事实并非如此。例如,您的 initialize
方法中的 score1
与 score1 = a [1]
。同样,对于 couplenumber
等。
You have a bunch of variables with the same name in different places, you seem to think these will be the same variable, but they aren't. For example, the score1
in your initialize
method is not the same as the one in the line score1 = a[1]
. Likewise for couplenumber
and so on.
由于前面的几点,您要插入的内容数组是一个空的Dancer对象,其实例变量均未设置为任何值。
Because of the previous points, what you're inserting into the array is an empty Dancer object with none of its instance variables set to anything.
以下是该数组的正确版本代码:
Here's a corrected version of the code:
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+")
# Splitting dance scores with "," and putting into arrays.
# Note that we're using map, which handles collecting the results into an array for us
dancescores = results.map |dancers|
a = dancers.split(",")
Dancer.new(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7])
# You could more simply write the previous line as
# Dancer.new(*a[0..7]), but that's
# essentially just syntactic sugar for the above
end
这篇关于Ruby-不会将文件加载到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!