私はかなり難しい時間を考え出していました。私はnilのために 'split'のエラーに遭遇しています:それはちょうど働いていたにもかかわらず、RubyのNilClass
in `process_data': undefined method `split' for nil:NilClass (NoMethodError)
は、だから私は、私が行った変更を元に戻したし、それが何度も何度もポップアップし、私はそれを引き起こしているものに関してはわずかな手掛かりを持っていない:エラーが言って現れましたか?
コード
class HiScore
attr_reader :hiscore
def initialize
@hiscore = if File.exist?('hiscore.txt')
read_file
else
build_new
end
end
def show
puts "Top 5 Scores"
@hiscore.each do |r|
puts r.each { |p| p }.join(" ")
end
end
def build_new
Array.new(5) {[1000, '--']}
write_file()
end
def read_file()
puts "read_file"
#File.open('hiscore.txt', 'r') #{|f| f.each_line.map{|l| l.chomp!.split(',')}}\
@hiscore = File.read("hiscore.txt").chomp!
process_data(@hiscore)
end
def process_data(instance)
@hiscore = @hiscore.split(",").to_a
@hiscore = @hiscore.each_slice(2).to_a
end
def check(score)
@hiscore.sort!{|s1, s2| s1[0] <=> s2[0]}
max = @hiscore[4][0].to_i
if score > max
puts "Sorry no top score for you."
else
add_name(@score.to_s)
# @hiscore.each do |row|
# p row[0] do |column|
# puts column[0].to_i
# end
# end
end
end
def add_name(score)
puts "You made it into the Top 5!"
print "Enter your name: "
@name = gets.chomp!.to_s
@hiscore.push([score, @name]).sort!{|s1, s2| s1[0] <=> s2[0]}.pop
#@hiscore = @scoreList
#@hiscore = @scoreList
write_file()
end
def write_file()
File.open('hiscore.txt', 'w') do |f|
@hiscore.each {|array| f.puts array.join(',')}
#print "#{@hiscore}"
end
end
end
def start
play = true
while play == true
num_guesses = 0
answer = rand(1..1000)
puts answer
@game.show
loop do
print "Type in your guess: "
guess = gets.chomp.to_i
num_guesses += 1
unless guess == answer
message = if guess > answer
"Too high"
else
"Too low"
end
puts message
else
puts "You got it!"
puts "It took you #{num_guesses} guesses."
@score = num_guesses
@game.check(@score)
break
end
end
print "Would you like to play again? (y/n): "
again = gets.chomp!
if again == 'n'
play = false
puts "Ok, Bye!"
exit
elsif again == 'y'
play = true
end
end
end
puts "I'm thinking of a random number from 1 to 1000"
puts "Can you guess it?"
@game = HiScore.new()
start()
は、誰もがこれを支援することはできますか?
ありがとうございました、私はこのことが起こる前に非常に混乱しました。私はその事実であるとは考えていませんでした。 – Alex