我 运行 遇到 'split' for nil:NilClass in ruby 的错误,即使它只是工作
I'm running into an error of 'split' for nil:NilClass in ruby even though it was just working
我正在处理一些我很难弄清楚的代码,然后突然出现这个错误说:
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()
有人可以帮忙吗?
错误的直接原因是 @hiscore
变量在 process_data
方法中尝试 split
-ed 时是 nil
。
我猜潜在的错误可能是 read_file
方法中的以下行:
@hiscore = File.read("hiscore.txt").chomp!
根据 documentation,chomp!
方法 returns nil
不对字符串进行修改时 ,即当它不从字符串末尾删除任何换行符时。如果末尾没有换行符,您可能想使用 chomp
而只是 returns 原始字符串。
如果 "hiscore.txt" 的值已经用逗号分隔并且您试图用“,”分隔符再次拆分它,它也可能会引发错误。
“@hiscore”是函数 read_file 的实例变量,因此范围将限于该函数本身。在 "process_data(instance)" 函数中,如果您使用实例代替“@hiscore”并且该变量具有正确的数据,那么它将正确处理。
我正在处理一些我很难弄清楚的代码,然后突然出现这个错误说:
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()
有人可以帮忙吗?
错误的直接原因是 @hiscore
变量在 process_data
方法中尝试 split
-ed 时是 nil
。
我猜潜在的错误可能是 read_file
方法中的以下行:
@hiscore = File.read("hiscore.txt").chomp!
根据 documentation,chomp!
方法 returns nil
不对字符串进行修改时 ,即当它不从字符串末尾删除任何换行符时。如果末尾没有换行符,您可能想使用 chomp
而只是 returns 原始字符串。
如果 "hiscore.txt" 的值已经用逗号分隔并且您试图用“,”分隔符再次拆分它,它也可能会引发错误。
“@hiscore”是函数 read_file 的实例变量,因此范围将限于该函数本身。在 "process_data(instance)" 函数中,如果您使用实例代替“@hiscore”并且该变量具有正确的数据,那么它将正确处理。