How to set class constants in Ruby?

A super beginner easily points to a ruby ​​question. I am trying to learn some rubies by programming Project Euler problems . Therefore i have a test

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

But this does not work, @solution is the nil when the test runs. What is the correct way to assign it in a class scope?

+3
source share
1 answer

Class constants in ruby ​​begin with a capital letter char:

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
+6
source

Source: https://habr.com/ru/post/1706088/


All Articles