Use Ruby command line tool out of the box upon gem install - ruby

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.

Related

Ruby: require works in gem, fails when running from source

Try creating a gem based on bundler's official guide on developing a Ruby gem.
Running bundle gem foodie will create a structure and generate files in the lib directory:
foodie
version.rb
foodie.rb
foodie.rb reads
require "foodie/version"
module Foodie
# Your code goes here...
end
Running ruby lib/foodie.rb (or also from different directories) will result in
C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- foodie/versio
n (LoadError)
from C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from foodie.rb:1:in `<main>'
On the other hand installing the gem via rake install and then requiring the gem works just fine.
It works from source if require "foodie/version" is changed to require_relative "foodie/version" in foodie.rb. As I understand
require works based on modules
require_relative works based on directory structure
To me the latter looks like a hack. It'd no longer make sense to structure your code via modules as it wouldn't be enforced (maybe it'd still make sense but you could make mistakes and never notice).
My questions are:
Is it possible to test a gem from source without installing it while following the bundler convention (using require instead of require_relative)?
Why does the gem work after installed?
Is there any best practice for the usage of require, require_relative, modules, files and general structure?
Thank you.
You need to add your lib dir to Ruby’s load path. The load path is a list of directories that Ruby searches for files in when you call require. Rubygems also manages the load path when you are using gems, which is why your code works when installed as a gem.
You say “as I understand ... require works based on modules”, this is not correct. require works with files, it’s just convention that a class or module is defined in a file with a matching name, e.g. MyModule might be in my_module.rb.
There are a few ways to add a dir to the load path. From the command line you can use the -I option:
$ ruby -I lib lib/foodie.rb
If you wanted to avoid typing -I lib you could use the RUBYLIB environment variable. Ruby adds the contents of this to the load path:
$ export RUBYLIB=lib
$ ruby lib/foodie.rb
(On Windows I think you will need to use set rather than export.)
You can also manipulate the load path from withing the program itself. It is stored in the global variable $LOAD_PATH, aliased as :$. This is how Rubygems and Bundler manage your gems.

How to make my ruby script shippable

I want to ship a ruby script and not ask the end consumer to run gem install or some bundler command. I just wants him to open the zip and run my script.
I understand that I will need to bundle my gems inside my zip but I am not sure how it can be achieved.
Today I am using bundler with the following .bundle/config:
---
BUNDLE_PATH: lib/vendor/bundle
BUNDLE_DISABLE_SHARED_GEMS: '1'
BUNDLE_FROZEN: '1'
and I do some nasty manipulation on library search path, in the beginning of my script:
$:.unshift File.dirname(__FILE__) + "/vendor/bundle/ruby/2.0.0/gems/colored-1.2/lib"
To conclude, What is the right way to create a statically linked (without external dependencies) ruby shippable script?
You're on the right track. You might want to look at bundle install --standalone, which generates a file that does all of the necessary load path manipulation.
http://myronmars.to/n/dev-blog/2012/03/faster-test-boot-times-with-bundler-standalone

Add ruby script in gem to path

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.

How to I load a gem from source?

I have git cloned a repo from Github, now I want to experiment with it, as in I want to poke around the code and mess with it. I've created a file test.rb that should load this gem, but I want to load my locally checked out version, what's the right way to do this?
Right now I'm just using a bunch of "require_relative 'the_gem_name/lib/file'", which feels wrong.
When you require 'foo' Ruby checks all the directories in the load path for a file foo.rb and loads the first one it finds. If no file named foo.rb is found, and you’re not using Rubygems, a LoadError is raised.
If you are using Rubygems (which is likely given that it is included in Ruby 1.9+), then instead of immediately raising a LoadError all the installed Gems are searched to see if one contains a file foo.rb. If such a Gem is found, then it is added to the load path and the file is loaded.
You can manipulate the load path yourself if you want to ensure a particular version of a library is used. Normally this isn’t something that’s recommended, but this is the kind of situation that you’d want to do it.
There are two ways of adding directories to the load path. First you can do it in the actual code, using the $LOAD_PATH (or $:) global variable:
$LOAD_PATH.unshift '/path/to/the/gems/lib/'
require 'the_gem'
Note that you normally want to add the lib dir of the gem, not the top level dir of the gem (actually this can vary depending on the actual Gem, and it’s possible to need to add more than one dir, but lib is the norm).
The other way is to use the -I command line switch to the ruby executable:
$ ruby -I/path/to/the/gems/lib/ test.rb
This way might be a bit cleaner, as normally you don’t want to be messing with the load path from inside your code, but if you’re just testing the library it probably doesn’t matter much.
Following apneadiving's suggestion in the comments, I created a Gemfile and added this line
source "http://rubygems.org"
gem 'gem_name', path: '~/path/to/gem/source/folder'
Then bundle install, and bundle exec ruby test.rb and it worked.

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.

Resources