gpt4 book ai didi

ruby - 如何在 ruby​​ 中设置类作用域常量?

转载 作者:数据小太阳 更新时间:2023-10-29 07:51:31 26 4
gpt4 key购买 nike

超新手易分ruby题。我正在尝试通过对 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。在类范围内分配它的正确方法是什么?

最佳答案

ruby​​ 中的 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

关于ruby - 如何在 ruby​​ 中设置类作用域常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/728417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com