After Installing a Gem Within a Script, How to Load the Gem

After installing a gem within a script, how do I load the gem?

With bundler version higher than 1.10 (to update just run gem install bundler) you can use its new 'inline' feature as described here.

require 'bundler/inline'

gemfile(true) do
source 'https://rubygems.org'
gem 'catpix'
end

puts Catpix::VERSION

First parameter of gemfile method is whether gems that aren't already installed on the user's system should be installed.

Ruby script cannot load a gem installed via bundler

If you just want to require this specific gem, require 'mysql' should work (e.g., https://github.com/tmtm/ruby-mysql/blob/master/test/test_mysql.rb#L10).

Install gem from ruby script and use it afterwards

After all, I solved the problem digging into gemianbox gem - it seems it uploads gem with a simple POST request, so gem inabox command isn't necessary at all, uploading can be achieved with a simple CURL command:

curl -F 'file=@/some/file.gem' https://some.host/upload

How to update a gem in a ruby script

Because using bundler in a single-file ruby script uses the latest constrained gem installed, in order to update one of the gems, you just have to run this (according to your example)

gem update colorize

Now your script will use the latest colorize gem version.

How to reference a local gem from a ruby script?

Make sure that your gem name as same as in Gemfile (e.g. custom_gem)

# Gemfile

source "https://rubygems.org"

gem "custom_gem", path: "/home/username/path/to/custom_gem"

Don't forget to actually install this gem using bundler

bundle install

After that, the script should be ready to use by bundle exec ruby script.rb

# script.rb

require 'custom_gem'
CustomGem::Do.something

How can I install a gem from a Ruby script?

How about:

require 'rubygems/dependency_installer.rb' 

?

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

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 Topics



Leave a reply



Submit