2009-04-08 4 views
3

スーパー初心者ルビーの質問です。私はProject Eulerの問題をプログラミングすることによっていくつかのルビーを学ぼうとしています。だから私はテストを持っていますruby​​でクラススコープ定数を設定するには?

class ProjectEuler_tests < Test::Unit::TestCase 
    @solution = 123456 # Not the answer so as not to be a spoiler 
    def test_problem_1 
    assert_equal(@solution, ProjectEuler1.new.solve) 
    end 
end 

しかし、これはうまくいかない、テストが実行されるときに@solutionはnilです。クラススコープでそれを割り当てる適切な方法は何ですか?ルビーで

答えて

6

Class constants大文字の文字で始まる:

class ProjectEuler_tests < Test::Unit::TestCase 
    SOLUTION = 123456 # Not the answer so as not to be a spoiler 
    def test_problem_1 
    assert_equal(SOLUTION, ProjectEuler1.new.solve) 
    end 
end 
関連する問題