Making an executable from ruby files using Ocra, LoadError? - ruby

I'm trying to make an executable from a couple ruby files on Windows, so I installed Ocra. I thought I understood the process of how Ocra works, but can't seem to get the executable working correctly. The problem I am having arises with "requiring" other ruby files.
The ruby program by itself compiles correctly and functions how I want it to, and Ocra seems to create a working executable, however, when I attempt to run the executable, I get the following error:
*/custom_require.rb:36:in 'require': cannot load such file -- MainMenuDialog.rb (LoadError)*
My main program is called 'JobManager.rb' and it is creating a new MainMenuDialog object, therefore I include MainMenuDialog.rb in the top of the file as such:
$: << File.expand_path(File.dirname(__FILE__) + "/../lib")
# Other requires here
require("MainMenuDialog.rb")
Again, the program compiles and runs perfectly fine by itself (including the require statements), and when I run the command ocra JobManager.rb it successfully runs the programs, checks for dependencies, and creates the executable. I just can't run the executable because of the "LoadError" described above.
Any thoughts on what I'm doing wrong? Thanks in advance!!!

With ruby >= 1.9 you may try require_relative.
Explanation: ocra stores all files in its own subdirectories.
If you manipulate the load pathes ($:) you can't be sure, what ocra uses during execution.

This question is old and answered, but I wanted to include this little nugget I found in Ocra documentation, because the correct answer here did not resolve my issue:
OCRA does not set up the include path. Use $:.unshift
File.dirname($0) at the start of your script if you need to 'require'
additional source files from the same directory as your main script.

Related

CLI tool built using commander gem doesn't execute correctly when used after being installed

So, I have a CLI tool I'm building using the commander gem.
The executable successfully executes correctly when used directly from the bin folder (bin/dynamised), but when I install the gem locally and then run it from the command line (dynamised) it doesn't seem to do anything.
If I add puts 'WORKING' to the top of the file, I see that but nothing else.
EDIT:
output of puts [$0, __FILE__].inspect:
from bin:
["bin/dynamised", "bin/dynamised"]
from installed gem:
["/Users/---------/.rbenv/versions/2.3.0/bin/dynamised", "/Users/---------/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/Dynamised-0.1.4/bin/dynamised"]
Link to gist containing executable.
Not quite sure what's wrong.
Change the very last line of your script to:
Dynamised::CLI.new.run if File.basename($0) == File.basename(__FILE__)
or simply remove this redundant check:
Dynamised::CLI.new.run

Is it possible Ruby installation by copying the ruby folder?

This is a very silly question...
I have a doubt regarding the ruby installation. Will the ruby be installed in a windows server if we copy the Ruby187 folder from another server and add that in the PATH of the environment variable?
Or Do we need to install the Ruby from the installer always, in order to install it ?
Thanks in advance
I actually did something similar to this yesterday at work. A fellow co-worker, who does not have Ruby installed on their system, needs the ability to run my scripts while I am on vacation. So, I copied the entire Ruby folder from my C: to a shared network drive.
You will not only have to add the path to the Ruby folder to your PATH variable but you also may want to associate the Ruby extension .rb with Ruby. Not needed but just a thought.
One issue I ran into was here at work we don't have permission to alter our PATH variable manually. So, in order for my co-worker to launch the scripts I needed ran, I wrote a small C++ app that merely run a command line call to the Ruby interpreter and then to the script to run.
So, in short, yes, it is possible. =)
EDIT: In regards to why you would add the path to the Ruby installation to the PATH variable, it is so you can call Ruby from the command line with simply C:\>ruby some_script.rb. Without that added to your PATH, you'd need to type the entire path every time like C:\>C:\ruby192\bin\ruby some_script.rb. However, you'd still need to type "ruby" first.
In regards to the association of ".rb" files to the Ruby Interpreter, it is an option while installing Ruby on Windows using the installer provided at ruby-lang.org. With that, you would not need to type "ruby" before the script name on the command line. C:\>some_script.rb would work. I don't know exactly how to do this with a network version of Ruby but one way might be to right-click on a ".rb" file, choose "Open with..." and locate the Ruby.exe file in \ruby192\bin\ruby.exe.
I hope that explains what you were asking about in the comments.

How to use remote files for classes/etc with Ruby?

I'm test SciTe editor, and Gosu for ruby game development. I decided to make a class to control my sprites. The class was written in a separate .rb file, in the same folder as the main .rb file. However, I can't use Sprite.new in the main file. How can I do that?
require 'sprite'
(Or whatever it's called.)
As Frederick notes, if you're using Ruby 1.9, the current directory isn't automatically part of the load path (not sure if I agree with the decision). You can add it on the command line like this:
ruby -I. main.rb
require File.join(File.dirname(__FILE__), '', 'MyFile')
I don't know why. But just require simply doesn't always work on different OS. But the above code always works for me.

Using a Windows executable within a Ruby gem

I'm looking to include a Windows .exe in my gem and call on that executable from within the gem. All the suggestions I've seen for including executable in gems calls for a hashbang to indicate which program should run the executable (typically "#!/usr/bin/env ruby
"). I don't know of any program to call; I simply want to call the .exe. What would be the best way to do this?
Those are for *nix systems.
You can do
%x{full path to your .exe}
Use
$?
to get the exit status.
http://www.ruby-doc.org/core-1.9.3/Kernel.html
The hashbang can make a gem executable but isn't necessary to simply call the .exe within the gem itself. The calling module was in the gem's lib/ and the .exe in bin/. I was able to call the .exe from the ruby code with
exe_path = File.expand_path(File.join(File.dirname(__FILE__), "..", "bin", "foo.exe"))
return_value = `#{exe_path}`
This works as long as you copy both lib/ and bin/ in the gemspec.

Deploying a Ruby project

I have a finished Ruby project that has the standard structure for a multiple file Ruby program:
project/
lib/ # Files the driver program uses go here.
bin/ # Driver program goes here.
tests/ # Unit tests go here.
What I want to be able to do is type in project into the command line from any directory and have my program run (which means it needs to be in my $PATH). My question is how do I do add a multiple file Ruby project to my PATH so that I can call by name in the terminal? (Or perhaps my approach is wrong, and I should do something else like make it into a Gem? I just don't know what is normally done).
package it as a gem and install that gem. If not and you're on linux then you can set your shebang line to be the right ruby, and chmod to make your script executable, and add your bin dir to your path (or what not).
You could go the quick and dirty route, and write a bash script that calls your main function to run the project, and then stick the bash script over in /usr/bin.

Resources