0

I've got a Ruby program that collects data into a friendly json format. I then pass that data into a python script by calling

`python3 py_script.py #{my_data}`

from the Ruby code.

This works well when running the Ruby script from the same directory as the python script. In short I'd love to pack this into a gem but after bundling and installing the gem, when I try to run it Ruby searches for the python file relative to where the current path of the shell is, and not from the structure specified in the gemspec, which is exactly what it does when just running the Ruby script directly.

I'm new to Ruby so this is entirely unexpected behavior. Is there a simple way to make this work? Or perhaps another method that I've just missed in my search?

For what it's worth I have tried several methods of calling the python script, using exec, Process.spawn, open3, system, etc.

1 Answer 1

1

Ruby will always have the path to the current script in __FILE__, and the path to its directory in __dir__. Thus, this is an easy fix:

`python3 #{__dir__}/py_script.py #{my_data}`

(You can obviously modify the path to fit your gem structure. Note that this is the path to the file it is contained in, not to the top of your gem.)

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I'd say that was an oversight on my part for such a simple change, but maybe my search phrases were just leading me to the wrong places. Is there a helpful overview somewhere on how Ruby generally handles this sort of thing, or maybe just a ruby-doc that describes __dir__ ?

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.