Ruby gems in stand-alone ruby scripts
You should be able to simply require it directly in recent versions of Ruby.
# optional, also allows you to specify version
gem 'chronic', '~>0.6'
# just require and use it
require 'chronic'
puts Chronic::VERSION # yields "0.6.7" for me
If you are still on Ruby 1.8 (which does not require RubyGems by default), you will have to explicitly put this line above your attempt to load the gem:
require 'rubygems'
Alternatively, you can invoke the Ruby interpreter with the flag -rubygems
which will have the same effect.
See also:
- http://docs.rubygems.org/read/chapter/3#page70
- http://docs.rubygems.org/read/chapter/4
Maintaining multiple standalone ruby scripts on same machine
How about using RVM or RbEnv?
In this case, you will need to have each file in a separate folder with rvm/rbenv config.
How to distribute a Ruby script with required gems
I'm operating off of the assumption that your required gems are in a Gemfile
. If you're having them run a bash script to run your Ruby script, have you considered adding bundle install
to the bash script prior to having it run your Ruby script? This will install all the required gems before running the script.
Another option would be to setup a Docker container that already has all the gems installed and just automatically runs the Ruby script. This would get you around issues with your users having the wrong version of Ruby, but has the added cost of having them install Docker.
Best options for deploying a Ruby standalone script and dependencies?
In my humble opinion, a gem is the way to go. Bundler makes it easy to get started; it starts a skeleton for you when you run the command…
bundle gem <GEM_NAME>
Take a look. As long as you specify your dependencies in your gem's .gemspec
file, and somebody installs your packaged gem (they won't need bundler, just RubyGems' gem
command), the dependencies will be installed as gems along with it.
Best way to describe the script dependencies in a Ruby Gem - where should I require?
If your gem doesn't work with Bundler out of the box by declaring:
gem 'mygem'
Then you've got to explain in the documentation why. Some gems, for intractable technical reasons, require you to load in specific files, or recommend that for some occasions. Bundler itself is one such gem:
require 'bundler/setup'
Where that will not only load Bundler, but will initiate the Bundler environment setup and imports the Gemfile
settings.
Normally your gem should have a main library file with the same name as the gem itself, and that takes care of loading all dependencies.
For example, mygem
looks like:
mygem.rb
mygem/some_module.rb
mygem/other_module.rb
Where the main mygem
looks like:
module MyGem
# ...
end
require_relative './mygem/some_module'
require_relative './mygem/other_module'
The require
calls are ordered so that any dependencies are properly accounted for.
If you have a module that has other dependencies of its own, the pattern repeats, like in mygem/submodule.rb
:
module MyGem::Submodule
# ...
end
require_relative './submodule/something'
Using a gem in a pure ruby script (not Rails)
Use bundler. Create a Gemfile
along side your ruby script.
In the Gemfile
, add:
gem "my-gem", git: "https://github.com/gem123.git", branch: "some-branch"
Make sure bundler is installed:
gem install bundler
And install the required gems:
bundle install
Now just initialize bundler at the top of your script:
require 'rubygems'
require 'bundler/setup'
# require your gems as usual
require 'my-gem'
Related Topics
Collecting Hashes into Openstruct Creates "Table" Entry
Marking an Unused Block Variable
Should I Deploy My Ruby on Rails Application on Heroku
How to Connect to Postgresql Database After Upgrading to Yosemite 10.10
Ruby: Eval with String Interpolation
How to Push Keys and Values into an Empty Hash W/ Ruby
Convert a Partial to Method/Block for Speed
How to Test If All Items in an Array Are Identical
Datetime with Mongodb/Mongoid and Rails 3 Not Populating
Convert 12 Hr Time to 24 Hr Format in Ruby
Devise Nomethoderror 'For' Parametersanitizer
Map an Array Modifying Only Elements Matching a Certain Condition
In Ruby What's the Difference Between Self.Method and a Method Within Class << Self
Ruby 2.0.0 String#Match Argumenterror: Invalid Byte Sequence in Utf-8