动态常量赋值Ruby

时间:2014-12-25 19:08:35

标签: ruby

我一直在关注创建打字挑战的教程。我小心翼翼地遵循这一点。当我尝试从命令行运行脚本时,我不断收到以下错误,我不明白。我认为这个教程可能已经很老了,但是如果有人能给我一些指导来理解它,那么我可以解决它,那将是非常感激的!我从命令行运行脚本时得到的错误如下....

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

1 个答案:

答案 0 :(得分:6)

以大写字母开头的名称是常量。在代码中,将非常量(动态)值分配给表示常量的名称。因此错误。

Console_Screen = Screen.new

使用本地变量名称conventionsnake_case

console_screen = Screen.new