超级初学者容易点红宝石问题.我正在尝试通过编程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
Run Code Online (Sandbox Code Playgroud)
但这不起作用,@ solution在测试运行时为零.在课程范围内分配它的正确方法是什么?
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
Run Code Online (Sandbox Code Playgroud)