Santi Aguilera
Santi Aguilera

Reputation: 85

Run from command line ruby method with return value

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

Upvotes: 0

Views: 577

Answers (2)

tadman
tadman

Reputation: 211540

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.

Upvotes: 0

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

Use Kernel#exit:

ruby -e 'require "file.rb"; exit MyClass.my_method?' 

Upvotes: 1

Related Questions