0

I have these lines on top of my Ruby code, and tried multiple combinations but none of them have worked.

$:.unshift File.dirname($0)
Dir.chdir(File.dirname($0))

I have a config file that is in the same directory than the exe created by Ocra. The file is loaded by this:

cnf = YAML.load_file('config.yml')

However, the file doesn't load from the same directory as wanted. The error, I think, tells it tries to load it from the temporary directory when the exe runs.

error

How can I get the script load the config.yml file from the same directory than the exe?

aksu
  • 5,221
  • 5
  • 24
  • 39
  • The [GitHub Page](https://github.com/larsch/ocra#working-directory) seems to address this type of issue fairly directly – engineersmnky Sep 14 '15 at 16:55
  • @engineersmnky still doesn't work :( Gives the same error even though I'm using the `--chdir-first` flag. – aksu Sep 14 '15 at 17:51
  • If I understand your question correct you expect, that `config.yml` is part of the compiled exe. Do you add `config.yml'` to your distribution? How do you call ocra? – knut Sep 01 '17 at 19:23

1 Answers1

1

Ocra uses an environment variable to store the location of the .exe ENV["OCRA_EXECUTABLE"] To access files relative to where your ruby_script.exe is you have to change your working to there. Here is some code that may work for you:

Dir.chdir File.dirname(ENV["OCRA_EXECUTABLE"]) if ENV["OCRA_EXECUTABLE"]

Having the "if ENV["OCRA_EXECUTABLE"]" at the end of this line keeps the script from throwing an error when your it is running without ocra as a ruby file (.rb). It simply checks if this ENV exists, if so then your program is running inside your ocra EXE

kaelhop
  • 401
  • 4
  • 5