0

I have the following class in Ruby in the file test_class.rb:

class TestClass

   def test_verify_one
         # DO SOME
   end

   def test_verify_two
         # DO SOME
   end

end

To execute this class I send two parameters to the terminal, ENVIRONMENT and LANGUAGE.

So... to call from terminal I use:

ruby test_class.rb ENVIRONMENT LANGUAGE

This executes both methods.

I want to execute only one.

I tried the following:

ruby -r "test_class.rb" -e "TestClass.test_verify_one" ENVIRONMENT LANGUAGE

but it is not working.

Can you help me?

6
  • 1
    is the def self.name_of_method the problem??? Commented Apr 13, 2016 at 15:28
  • 3
    "This executes both methods" - no, it doesn't, given the code that you show. Nothing is executed here. Commented Apr 13, 2016 at 15:33
  • @SergioTulentsev, you were right... that is something that I added to try a new approach. I updated the code. Commented Apr 13, 2016 at 15:37
  • Still it won't execute any code. Commented Apr 13, 2016 at 16:20
  • 1
    What @Sergio means is that, to execute your code, you need to add lines such as the following after your class definition: t = TestClass.new; t.test_verify_one; t.test_verify_two. Commented Apr 13, 2016 at 16:41

2 Answers 2

4

Within the same folder as test_class.rb, run the ruby command with the following command syntax:

ruby -I . -r "test_class" -e "TestClass.test_verify_one" arg1 arg2

Breaking this command down we get:

-I . Include current directory in $LOAD_PATH so we can use require

-r Require a file named test_class within the $LOAD_PATH. This is possible because we included the current directory in our load path above (.rb extension is optional here).

-e Same as you've provided, evaluates the following code.

Now, if we call ARGV within that method we should get arg1 and arg2 on separate lines:

#...
def self.test_verify_one
  puts ARGV
end
#...
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Anthony E Now I am having this issue: -e:1:in <main>': undefined method test_verify_one' for TestClass:Class (NoMethodError)
Did your original question use a class method? In your case it would be -e TestClass.new.test_verify_one. Doesn't matter if you use a class or instance method here
I did @Anthony, I tried with ruby -I . -r "test_class" -e "TestClass.new.test_verify_one" arg1 arg2 and also with ruby -I . -r "test_class" -e "TestClass.new(test_verify_one)" arg1 arg2 but I get: C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/test-unit-3.0.8/lib/test/unit/testcase.rb:400:in initialize': wrong number of arguments (0 for 1) (ArgumentError) from -e:1:in new' from -e:1:in <main>'`
Your test_class filename may be colliding with one of the core classes of ruby. Try rename both your file and class to something else.
0

If you want your first method to take the variables you're passing to it you need to define your first method to take two variables, like so:

class TestClass

    def test_verify_one (var1, var2)
             # DO SOME
       end

       def test_verify_two
             # DO SOME
       end

end

You will then need to put a condition into your second which causes it to only execute under the conditions you want it to... or just comment it out if you don't need it at the moment. So, for example, if you only wanted test 2 to fire when variables were not passed in, your code would look something like this:

class TestClass

    def test_verify_one (var1, var2)
        # DO SOME
    end

    def test_verify_two (var1, var2)
        if (var1 and var2) defined? nil
        # DO SOME
    end

end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.