tags:

views:

104

answers:

2

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?

+5  A: 

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
dbarker
+2  A: 

Class variables in ruby are designated by "@@"

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

Edit: I missed the "constant" in the title

Joe Zack
Isn't this the same as a static field in C#? (If there is such a concept in ruby)
George Mauer