2014-12-25 109 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:如果没有真正运行/调试该脚本,很难说。试着弄明白。如果你决定为生活编码,那么你会做很多事情:) –