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
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.
A few problems here:
The method is called
initialize, notInitialize— capitalization is important.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
score1in yourinitializemethod is not the same as the one in the linescore1 = a[1]. Likewise forcouplenumberand so on.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: