我遇到了错误的错误' split'为nil:即使它只是工作,红宝石中的NilClass

时间:2016-05-15 18:14:16

标签: ruby multidimensional-array file-io nomethoderror

我正在研究一些代码,我很难搞清楚这个错误出现在哪里,然后说不出来:

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()

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

错误的直接原因是@hiscore方法中的nil变量为process_data时尝试split编辑。

我猜基础错误可能是read_file方法中的以下行:

@hiscore = File.read("hiscore.txt").chomp!

根据documentationchomp!方法在不修改字符串时返回nil,即当它不剥离任何换行符时字符串末尾的字符。您可能希望使用chomp代替原始字符串,如果它在结尾没有换行符。

答案 1 :(得分:0)

如果“hiscore.txt”的值已经以逗号分隔并且您尝试使用“,”分隔符再次将其拆分,则它也可能抛出错误。

“@ hiscore”是函数read_file的实例变量,因此范围将仅限于该函数本身。在“process_data(instance)”函数中,如果您使用“@hiscore”的实例,并且该变量具有正确的数据,那么它将正确处理。