I am trying to create a ruby script to help me run some C++ tests on a linux terminal. I am a total beginner when it comes to Ruby, and am running into a few problems. I have defined a global variable array called "tfailed", but get this error when I try pushing into it:
mscr.rb:18:in `runTest': undefined method `-@' for ["Card_test_all"]:Array (NoMethodError)
The offending bit of code is here:
$trun=0;
$tpassed=0;
$tfailed=Array.new;
def runTest(testname)
puts "Running #{testname}";
if system("make " + testname + ".exe > /dev/null") then
puts "#{testname} compiled correctly.";
end
succ=system("./"+testname+".exe > /dev/null");
$trun=$trun+1;- #typo was here
if(succ)
$tpassed=$tpassed+1;
puts (testname + " successfully run.");
else
puts (testname + " unsuccessfully run.");
$tfailed << testname; #LINE 18 here
end
puts "-------------------------------";
end
If the C++ program doesn't run successfully, the name of the test should be pushed into the array. In this case, the function called was
runTest("Card_test_all");
The code should be pushing the string "Card_test_all" into the $tfailed array, but it gives me the above error instead. Any help or ideas on how to fix this would be appreciated.
Edit: Added the line number that causes the problem. Also, thanks to the commentors below for the ruby style/syntax advice.
Edit 2: The typo I edited out in my first change actually was the problem all along. I'm an idiot. Thanks for the help though.
-in$trun=$trun+1;-?system. Someone will complain about all those unnecessary semicolons so I'll do it: you don't need semicolon terminators in Ruby. Be careful when using parentheses to call methods,m (x)andm(x)are very different things. You might want to include a bit more whitespace:$trun = 0and$trun = $trun + 1for example.["Card_test_all"]by the time your exception occurs...+consider using string interpolation, so you can do things like:"#{testname} successfully run."