Using a gem in a pure ruby script (not Rails) - ruby

A ruby file:
gem "my-gem", git: "https://github.com/gem123.git", branch: "some-branch"
require "my-gem"
var1 = SomeGem::some_method123
puts var1
It says Could not find 'my-gem' (>= 0) among 330 total gem(s) (Gem::LoadError). Why not? I need a special branch of a gem and don't want to clone the repository.

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

How to update a gem in a ruby script

I have a ruby script with the requisite gems specified within it e.g
#!/usr/bin/env ruby
require 'bundler/inline'
require 'matrix'
gemfile do
source 'https://rubygems.org'
ruby '2.7.3'
gem 'colorize'
gem 'pry'
end
puts "warning".colorize(:red)
Normally to update a gem, I would type something like bundle update colorize, but this returns an error
Could not locate Gemfile
So how do I update a gem in this script. Is there an equivalent of a Gemfile.lock that I can list?
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.
According to the docs running the file will install the dependencies. Without a lock file being generated, though, you might want to manually specify versions in your gemfile block:
gemfile do
source 'https://rubygems.org'
ruby '2.7.3'
gem 'colorize', '~> 0.8.1'
gem 'pry', '~> 0.14.1'
end
Your script likely won't be compatible with all versions of a library, so it's probably a good idea to add version constraints when there's no lock file. Then when you want to upgrade, you update the number and Bundler should install the new version.

Do we need require and Gemfile at the same time?

In a ruby program, there is a separate Gem file, which contains the following definitions:
source "https://rubygems.org"
gem "typhoeus"
gem "json"
gem "pg"
gem "google_drive" , "2.1.11"
gem "mandrill-api"
If I do not want this gem file for some reason, then in ruby script, I need to add require for all the libraries,
such as:
require typhoeus
require json
require pg
require google_drive, 2.1.11
require mandrill-api
will this work?
The purpose of the Gemfile can be helpful so that you can insure your code will work by using bundler which allows you to run bundle install which will install the gems to work with the current version of ruby you will be using for your code. It will also add a Gemfile.lock file which is a good idea to commit in your version control to insure you have a working stack where the gems and the ruby version are all compatible.
If you only require the files in your script, there is no guaranteed that the gems are actually installed in the scope of that script's runner. So by having a Gemfile and Gemfile.lock and using bundler, you can have portability for your codebase.
Update
As per comment by #engineersmnky , you can specify gem version however with this syntax and it should work so long as those gems are installed. You would first need to make sure to install the version in your terminal:
gem install google_drive -v 2.1.11
Then you can do this in your ruby file
require 'rubygems'
gem 'google_drive', '2.1.11';
require 'google_drive'
require 'typhoeus'
require 'json'
require 'pg'
require 'mandrill-api'

Gemspec resolve dependencies

I wanted to create a gem with some dependencies. I followed a tutorial, here is the full code.
I have
s.add_dependency "sinatra"
In the gemspec. I build the gem. When I tried to install it with
gem install --local gemname.gem
I got
ERROR: Could not find a valid gem 'sinatra' (>= 0) in any repository
I instead expected that gem install will first install sinatra and then proceed with my gem.
How can I make it install any dependencies prior to my gem? I tried to:
add gem 'sinatra' to the gemfile
use add_runtime_dependency instead of add_dependency
require 'rubygems' on top of my gemspec file
TL;DR:
gem install gemname.gem # NO --local switch
When given, --local restricts all the actions to local domain, as clearly written in gem help install output:
Local/Remote Options:
-l, --local Restrict operations to the LOCAL domain
That said, the above will succeed if and only sinatra is already available in local.

How to install gems from Gemfile?

I want to add code coverage to my project and sign up coveralls.io and create Gemfile with:
gem 'coveralls', require: false
but how can I install the gem from Gemfile?
run the command bundle install in your shell, once you have your Gemfile created.
This command will look your Gemfile and install the relevant Gems on the indicated versions.
The Gemfiles are installed because in your Gemfile you are pointing out the source where the gems can be downloaded from.
Your can create a Gemfile just by typing bundle init in your shell
I add a Gemfile example for your reference:
source "https://rubygems.org" # where gems will be downloaded from
ruby "2.2.3" # ruby version, change for the one you use
gem "sinatra"
gem "sinatra-flash"
gem "sinatra-partial"
gem "bcrypt"
gem "dm-validations"
gem "dm-transactions"
gem "data_mapper"
gem "dm-postgres-adapter"
gem "pg"
gem "database_cleaner"
group :test do # you can make groups for test, development, production..
gem "rspec"
gem "capybara"
gem "rspec-sinatra"
gem "cucumber"
gem "coveralls", require: false
end
First install bundler if you don't have it
gem install bundler or sudo gem install bundler if you don't have the required permissions. Bundler is a gem that manages gem dependencies.
then you can follow the above instruction for creating the gemfile, after which you can issue the command
bundle install

How do I install a Ruby gem from GitHub and have it work in IRB?

Seems like every Ruby tutorial I find centers around Rails.
Anyway, I simply want to install a gem from a GitHub repo and have that gem work in irb.
I want to install the exifr gem. When I do a gem install exifr it doesn't get the newest version.
So I created a Gemfile and put:
gem 'exifr', :git => 'git://github.com/remvee/exifr.git'
Then bundle install. Installs OK but now gem list doesn't find the gem. So I can't require it in irb.
Any help for NON Rails applications?
Thanks
You need to kick off the bundler setup if you want to use the gems from a gemfile:
require 'bundler/setup'
require 'exifr'
or:
irb -rbundler/setup
> require 'exifr'
This is equivalent to running bundle exec irb, except it doesn't depend on a specific invocation to work, and instead presumes that a Gemfile is available and the gems were installed with Bundler.
Try...
bundle exec gem list
And..
bundle exec irb -rexifr
And see if that works. That should help gem/irb find the installed gem.

Resources