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:

doThing() {
    echo "Doing Thing"

source ./

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


def doThing
    puts "Doing Thing"

source ./test1.rb


>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