2016-05-15 13 views
0

私はかなり難しい時間を考え出していました。私は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() 

は、誰もがこれを支援することはできますか?

答えて

1

split -edにしようとすると、変数がのprocess_dataメソッドにあるという直接的な原因です。

私は根本的なエラーがread_file方法で次の行であるかもしれないことを推測:

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

documentationによると、それは文字列に一切の変更を行うものではありませんときnilを返すchomp!方法、すなわち文字列の末尾にある改行文字は取り除かれません。末尾に改行がない場合は、元の文字列を単に返す代わりにchompを使用します。

+0

ありがとうございました、私はこのことが起こる前に非常に混乱しました。私はその事実であるとは考えていませんでした。 – Alex

0

"hiscore.txt"に値がすでにカンマ区切りになっていて、 "、"区切り記号でもう一度分割しようとすると、エラーが発生することもあります。

"@hiscore"は関数read_fileのインスタンス変数であるため、スコープはその関数自体に限定されます。 "process_data(instance)"関数で、 "@hiscore"のインスタンスインプレースを使用し、その変数が正しいデータを持っていれば、それは正しく処理されます。

関連する問題