Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set class-scope constants in ruby?

Tags:

ruby

constants

Super-beginner easy points ruby question. I'm trying to learn some ruby by programming the Project Euler problems. So 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 doesn't work, @solution is nil when the test runs. What is the proper way to assign it at the class scope?

like image 612
George Mauer Avatar asked Dec 29 '25 06:12

George Mauer


1 Answers

Class constants in ruby start with an uppercase 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
like image 78
dstnbrkr Avatar answered Jan 01 '26 18:01

dstnbrkr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!