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.
Related
I made a bash command line tool that I'd like to convert to Ruby.
I know that I'd have to use OptionParser and stuff to write the program, but I look at programs like rake and see that once you install the gem, it's ready to use immediately. Why is this? When I make a program with optparse I have to put it in my bin and give it access.
How can I have the user use it out of the box with mac or windows if I make it into a gem?
Thank you
Gems can include executable files which are added to the user's PATH by RubyGems when installing the gem.
Usually, those scripts are put into the bin directory (or exe nowadays) of your gem. You can then specify in your gemspec that scripts in this directory should be treated as executables:
In your gemspec file, you can thus put something like this:
Gem::Specification.new do |spec|
spec.name = 'my_awesome_gem'
spec.version = '0.0.1'
spec.bindir = 'bin'
spec.executables = ['my_script']
# ...
end
As for the script itself, you should make sure that it is marked as executable (i.e. chmod +x bin/my_script on Linux/Mac) and that it has the right shebang as its first line. Usually, it looks like this:
#!/usr/bin/env ruby
puts 'Hello World'
You can learn more about adding executables to your gem in the RubyGems guide.
Finally, if you are creating your basic gem structure with the bundle gem my_awesome_gem command, it will automatically create a reasonable gemspec file and basic structure. Just put your scripts into the exe directory and everything should just work.
I am trying to create an executable gem on windows machine. But i am not sure how do make the file executable in windows? Is there any way i can create the same on windows? Here is what i did so far: created a file and then right click and update the permission to read and execute. But its not working. I am keeping this file under bin folder as mentioned " here " in the link
A gem is not executable itself, it is a bundled collection of Ruby scripts and other files which can be executable. You can compare it with a Zip file.
The purpose is to easily distribute your projects tot other computers.
When you gem install the gem and require the gem in a script then you can use what the gem has to offer.
It is possible to execute the scripts and executable files when you use the full path in your console or when the path is included in the Windows path environtment variable. Sometimes a .bat or .cmd (in Windows) is provided that can be called but it just executes some other Ruby scripts.
I just created a very simple rubygem which has only one file that takes a couple of parameters.
I want to automatically add this ruby script to the path when I install it so that i can use it from anywhere in terminal like:
myruby "param1" "param2"
Have a look at this documentation from RubyGems.
Adding an executable to a gem is a simple process. You just need to place the file in your gem’s bin directory, and then add it to the list of executables in the gemspec. Let’s add one for the Hola gem. First create the file and make it executable:
This article also seems to be pretty good and it covers the essential details of adding an executable.
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.
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.