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.
How can I get the script load the config.yml file from the same directory than the exe?
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
Related
I have a program that exists in the /usr/local/flower directory - flower.rb. It requires loading libraries that also exist in the same directory. I've included this flower directory in my OS environment's path. And my choice of OS is Ubuntu.
When I execute flower.rb from any other directory other than /usr/local/flower, I get error messages indicating the program can't load the libraries that also exist in the /usr/local/flower directory b/c they are being loaded as ./[library] from source code.
I realize I could change the Ruby program to hard code the /usr/local/flower/[libraries], but I'm curious if there's a way to execute this program from my home directory, e.g. /home/seattle, w/o doing this.
Also, when the program executes, it creates output via the -o switch.
My solution was to create a bash script that changed to the /usr/local/flower directory, executed the program, and then return to the PWD directory from where the flower.rb program was called. The problem is the -o switch. If I do a -o [file] the [file] gets written to the /usr/local/flower directory as opposed to where I am when the flower.rb program is run.
What is a good solution for this problem?
It's not quite clear from your question, but it appears that you are using require wrongly. require is for loading scripts from the $LOAD_PATH. If you want to load a script relative to the directory of the current script, use require_relative.
You should never load scripts relative to the current working directory. The current working directory is under the control of the user, not your script. You have no idea what it is gonna be. There is a reason why the current working directory was removed from the $LOAD_PATH in 2008: because it's broken.
I've had a lot of trouble getting one my wxruby scripts to use an image file I included in the exe with ocra. If I didn't have the original image file in the same directory as the exe, the exe wouldn't find the image.
shape = File.join('warning3.png' )
I wanted the script to find the image I included in the exe.
In the ocra documentation it mentions Dir.chdir File.dirname($0) but I didn't get what it meant when it was mentioned.
If you need to use say an image file in your script you can make sure it works merely by having this Dir.chdir(File.dirname($0)) line before you try to use any of your images.
For example in my app I'm using an image file and I couldn't get my exe to work if it wasn't in the same dir as the file but with the below it works anywhere so far.
Dir.chdir(File.dirname($0))
shape = File.join('warning3.png' )
I think this sets the script's current directory to the exe's directory inside it? If I'm wrong please let me know!
I want my Ruby Script File to run as executable from any directory of Windows XP. I have created a test.rb (Ruby Script file) and want to run it from any directory of my Windows as "test" for example, "C:\test" or "C:\Directory\test" runs my file test.rb.
#!/usr/bin/envy ruby
p "Hi this is my test file"
I have added the shebang code in my ruby file but when I have to run the Ruby Script, I have to locate my Script file and run it expicitly as "ruby test.rb".
I have also made the file executable by executing the command:$ chmod +x hello-world.rb
, but it still does not work.
Thanks in advance.
I assume you're using Linux or OS X and creating the file on a disk accessible from Windows? Windows does not use shebangs, and it does not use Unix file modes. You will need to associate files with the .rb extension to the Ruby executable; details for that operation can be found at this Stack Overflow question; once you have done this, you can run C:\whatever\test.rb or C:\whatever\test to execute the script.
The following .rb script runs fine if excuting at the script's folder:
db = YAML::load(File.open('db.yml'))
ActiveRecord::Base.establish_connection(db)
The File.open will fail if the script is running outside the script folder. How can I supply the script's path to db.yml? Thanks!
This should work:
db_file = File.join(File.dirname(__FILE__), "db.yml")
Edit: I got a little bit confused with the script folder, this should work now.
If you find yourself wanting to do this a bunch, you might consider adding the script's directory to your load path (especially in 1.9.2 where "." is no longer in the load path):
$: << File.expand_path(File.join(File.dirname(__FILE__)))
I am trying to read in from a few files using something like this
IO.foreach("TeamFields.txt") { |line| fieldNames.push(line.chomp) }
It works fine when running the from the command line, but when I package to an .exe with shoes and run it can't find the file. Is there a way to specify a path relative the the .exe or do I have to provide the full filepath (e.g. "c:\files\TeamFields.txt")? Thanks for the help.
This is because your executable is not run with the correct current directory.
Either fix the current directory (for example in the shortcut) or modify your Ruby program to automatically set the working directory to the program directory with:
Dir.chdir(File.dirname($PROGRAM_NAME))
You need to set "Current Application Directory" correctly before going relative.
The user can execute your app with different start up dir, or system can call your app with different dir.
If files in question are in the folder of your app, the only thing you need to do is to get that folder, and set it to be current.
I don't program in ruby, but I do with windows, and odds are the relative path will be based on the location of the .exe file.
So, yes, you're probably better off passing a full path for the file name.
The constant __FILE__ will contain the full path to the currently executing file. You can then use methods of the File class to strip off the filename, append the relative path for whatever other file in your package it is you want and resolve the result.