0

我对 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: , , , , , , .

我不太擅长编码,但仍在努力学习:-) 在此先感谢。

4

4 回答 4

6

你错过了将论据传递给Dancer

我还建议将您的代码重构为如下所示:

class Dancer
  def initialize( couplenumber, *scores )
    @couplenumber, @scores = couplenumber, scores
  end

  def show
    return "Couple Number: #{@couplenumber}. Scores: #{@scores.join(', ')}"
  end
end

file = "danceresult.txt"
dancescores = File.readlines(file).map do |line|
  Dancer.new *line.split(",")
end
于 2012-11-28T17:49:39.593 回答
3

您的第一个问题是一个错字:该方法应该命名为initialize,而不是Initialize

我还建议您查看Ruby 的 CSV 库,它可以更可靠地解析 CSV 数据(并且更惯用)。

我的最后一个建议是不要定义show,而是使用to_s. 然后你可以简单地puts dance和对象本身知道如何转换为字符串。

于 2012-11-28T17:50:15.350 回答
1

当你初始化你的 Dancer 对象时,你并没有传入一对数和分数。

线

dancescores << Dancer.new

应该

dancescores << Dancer.new(couplenumber, score1, score2, score3, score4, score5, score6, score7)

请注意您存储的值是如何传递给 Dancer 的初始化的。

于 2012-11-28T17:49:33.510 回答
1

这里有几个问题:

  1. 该方法被称为initialize,而不是Initialize- 大写很重要。

  2. 你在不同的地方有一堆同名的变量,你似乎认为这些是同一个变量,但事实并非如此。例如,score1in your initializemethod 与 line 中的不一样score1 = a[1]。以此类推couplenumber

  3. 由于前面的几点,您要插入到数组中的是一个空的 Dancer 对象,其实例变量均未设置为任何值。

这是代码的更正版本:

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
于 2012-11-28T17:51:14.413 回答