In Ruby, how can I get ARGV to read my input as a variable instead of a string?

StackOverflow https://stackoverflow.com/questions/23371436

  •  12-07-2023
  •  | 
  •  

Frage

I have an assortment of test cases that I want to run. The dumbed-down code is like this:

class TestCase
    def initialize(caseName)
        name = caseName
        ...a bunch more code...
    end
end    

@TC001 = new TestCase("Case 1")
@TC002 = new TestCase("Case 2")
...
@TC100 = new TestCase("Case 100")

Then I'm trying to pass in to the command line which test cases I need to run this time. The Runner file looks something like:

testCaseArray = Array.new
i = 0
while i < ARGV.length
    testCaseArray < ARGV[i]
    i += 1
end

runTestCases(myArray)
    myArray.each do |thisCase|
        puts thisCase.name
     end
end

runTestCases(testCaseArray)

So then when I go to the command line and enter:

ruby Runner.rb @TC027 @TC030 @TC075

I get an error because it's not recognizing @TC027 as a variable but is instead recognizing it as a string, and (string).name isn't valid.

How do I get it to read the string as a variable?

War es hilfreich?

Lösung

You can look up an instance variable named by a string with instance_variable_get.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top