How to source ruby functions in bash script

In bash, if I have two scripts, one with a function and the other using the first script as a source, then I can access the function in the second script. Can I do something similar with ruby?

I can do this:

test1.sh

doThing() {
    echo "Doing Thing"
}

test2.sh

source ./test1.sh
doThing

Is there a way to do something similar with ruby?
The following fails.

test1.rb

def doThing
    puts "Doing Thing"
end

test2.sh

source ./test1.rb
doThing

Thanks

>Solution :

The quick answer is "no", but you can call ruby programs from bash just like you can any other executables:

  • Create a ruby program that takes arguments and calls the desired functions based on those arguments.
  • Compile that program if you compile Ruby – I don’t recall.
  • Create a bash script that defines a bunch of functions, each of which call your ruby program with the appropriate arguments to cause it to call it’s functions.
  • Source that bash script of functions into your main bash script.
  • Call the bash functions from that sourced script.

Leave a Reply