0

I have a ruby code like this:

module MyClass
    def self.my_method?
        return false
    end
end

From command line I have to call that method and get the return value. But if I do

ruby -e "require './PATH/TO/THE/FILE/file.rb'; MyClass.my_method?"

I always get $? or exit code = 0.

How can I call ruby methods GETTING its return value ?

Thanks

2 Answers 2

1

Use Kernel#exit:

ruby -e 'require "file.rb"; exit MyClass.my_method?' 
Sign up to request clarification or add additional context in comments.

Comments

0

This is a highly unusual way to call Ruby code, but if you do need to do it:

ruby -r ./file.rb -e 'exit(MyClass.my_method? ? 0 : -1)'

That converts a boolean response to something explicitly passed through to exit where you can collect it as an exit code. You can map your result to some kind of error code if necessary. I've just used -1 as an example here.

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.