【问题标题】:How to set class-scope constants in ruby?如何在 ruby​​ 中设置类范围常量?
【发布时间】:2009-04-08 03:25:52
【问题描述】:

超级初学者易分红宝石题。我试图通过编程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 在测试运行时为零。在类范围内分配它的正确方法是什么?

【问题讨论】:

    标签: ruby constants


    【解决方案1】:

    Class constants 在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 2013-08-08
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多