Ruby - 不会将文件加载到数组中

时间:2012-11-28 17:43:10

标签: ruby arrays

我遇到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 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

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

我还建议您研究一下Ruby's CSV library,它可以更有效地解析CSV数据(并且更加惯用)。

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

答案 2 :(得分:1)

当你初始化你的Dancer对象时,你并没有传递这对夫妇的数字和分数。

该行

dancescores << Dancer.new

应该是

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

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

答案 3 :(得分:1)

这里有一些问题:

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

  2. 你在不同的地方有一堆同名的变量,你似乎认为这些变量是同一个变量,但它们不是。例如,score1方法中的initialize与行score1 = a[1]中的couplenumber不同。同样适用于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 等等。

  3. 由于前面的几点,你在数组中插入的是一个空的Dancer对象,它的实例变量都没有设置为任何东西。

  4. 以下是代码的更正版本:

    {{1}}