如何在ruby中设置类范围常量?

时间:2009-04-08 03:25:52

标签: ruby constants

超级初学者容易点红宝石问题。我试图通过编程Project Euler问题来学习一些红宝石。所以我有一个测试

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在测试运行时为零。在课程范围内分配它的正确方法是什么?

1 个答案:

答案 0 :(得分:6)

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