2014-12-25 142 views
3

我一直在關注教程來創建一個打字挑戰。我已經小心謹慎地遵循這一點。當我嘗試從命令行運行腳本時,我不斷收到以下錯誤,但我不理解它。我認爲這個教程可能會很老,但如果有人可以給我一些指導來理解它,所以我可以修復它,那麼將非常感激!當我運行在命令行如下腳本我得到錯誤....動態常量分配紅寶石

Typechallenge.rb:89: dynamic constant assignment 
    Console_Screen = Screen.new 
         ^
typechallenge.rb:90: dynamic constant assignment 
    Typing_Test = Test.new 

腳本本身低於...

#Script name: Typing Challenge 
#Description: Demonstrating how to apply conditional logic in order to analyze user input and control 
#the execution of the script through a computer typing test. 
class Screen 

def cls 
    puts ("\n" * 25) 
    puts "\a" 
end 

def pause 
    STDIN.gets 
end 

end 

class Test 

def display_greeting 

    Console_Screen.cls 

    print "\t\t Welcome to the Typing Challenge" + 
    "\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " + 
      "continue. \n\n: " 

    Console_Screen.pause 

end 

def display_instructions 

    Console_Screen.cls 
    puts "\t\t\tInstructions:\n\n" 

    puts %Q{ This test consists of five typing challenges. Each sentence is a challenge and are presented one at a time. To respond 
     correctly you should retype each sentence exactly as it is shown and the press the Enter key. Your grade will be displayed at 
     the end of the test.\n\n\n\n\n\n\n\n\n 
     Press Enter to continue.\n\n} 

     Console_Screen.pause  

    End 

    def present_test(challenge) 

     Console_Screen.cls 
     print challenge + "\n\n: " 
     result = STDIN.gets 
     result.chop! 

     if challenge == result then 

      $noRight += 1 
      Console_Screen.cls 
      print "Correct!\n\nPress Enter to continue." 
      Console_Screen.pause 

     else 

      Console_Screen.cls 
      print "Incorrect!\n\nPress Enter to continue." 
      Console_Screen.pause 

     end 

    end 

    def determine_grade 

     Console_Screen.cls 

     if $noRight >= 3 then 
      print "You retyped " + $noRight.to_s + " sentence(s) correctly. " 
      puts "You have passed the typing test!\n\nPress Enter to continue." 

     else 

      print "You retyped " + $noRight.to_s + " sentence(s) correctly. " 
      puts "You have failed the typing test!\n\nPress Enter to continue." 
     end 
    end 

    #Main script logic 

    $noRight = 0 

    Console_Screen = Screen.new 
    Typing_Test = Test.new 

    Typing_Test.display_greeting 

    Console_Screen.cls 

    print "Would you like to test your typing skills? (y/n)\n\n: " 

    answer = STDIN.gets 
    answer.chop! 

    until answer == "y" || answer == "n" 

     Console_Screen.cls 

     print "Would you like to test your typing skills? (y/n)\n\n: " 

     answer = STDIN.gets 

     answer.chop! 

    end 

    #Analyzing the players response 

    if answer == "n" 

     Console_Screen.cls 
     puts "Okay, perhaps another time! \n\n" 

    else 

     Typing_Test.display_instructions 
     Typing_Test.present_test "In the end there can be only one" 
     Typing_Test.present_test "Once upon a time a great plague swept across the land" 
     Typing_Test.present_test "Welcome to the typing challenge" 
     Typing_Test.present_test "There are very few problems in the world" + "that enough M&Ms cannot fix." 
     Typing_Test.present_test "Lets play this game of life together" 

     Typing_Test.determine_grade 

     Console_Screen.pause 

     Console_Screen.cls 
     puts "Thank you for playing the game!\n\n" 

    end 

end 
end 

回答

6

名稱以大寫字母開頭是常數。在您的代碼中,您將一個非常量(動態)值分配給代表常量的名稱。因此錯誤。

Console_Screen = Screen.new 

使用本地變量名conventionsnake_case

console_screen = Screen.new 
+0

感謝我只是困惑,爲什麼一本教程會那樣做,你有什麼想法?我已經改變建議,它刪除了錯誤,所以謝謝,但是當我嘗試運行腳本現在沒有任何反應,所以任何進一步的建議將不勝感激! – Lilp

+0

我無法識別你.. :)聖誕快樂! –

+0

@emma:如果沒有真正運行/調試該腳本,很難說。試着弄明白。如果你決定爲生活編碼,那麼你會做很多事情:) –