Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading