Do we need require and Gemfile at the same time? - ruby

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'

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.

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

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'

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.

Bundle don't works with mongo_ext

I have a problem with a very simple Gemfile:
source :rubygems
gem 'mongo'
gem 'mongo_ext'
I installed the gems with "bundle install" but it doesn't load mongo_ext.
irb(main):001:0> require 'rubygems'
=> false
irb(main):002:0> require 'mongo'
**Notice: C extension not loaded. This is required for optimum MongoDB
Ruby driver performance. You can install the extension as follows:
gem install bson_ext
If you continue to receive this message after installing, make sure that the
bson_ext gem is in your load path and that the bson_ext and mongo gems are of
the same version.
=> true
But if I use the system irb I it is load:
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'mongo'
=> true
irb(main):003:0>
Maybe that behavior is because mongo_ext includes C extensions.
You need to add bson and bson_ext to your Gemfile:
source :rubygems
gem 'mongo'
gem 'mongo_ext'
gem 'bson'
gem 'bson_ext'
And generally speaking, it's a good idea to specify versions of the gems you are using. That way you can ensure your code works even if a gem makes breaking changes (or adds new bugs that affect you). Specify the newest version that is out at the time you start your project, but upgrade them only with care. Example:
source :rubygems
gem 'mongo', '1.5.1'
gem 'mongo_ext', '0.19.3'
gem 'bson', '1.5.1'
gem 'bson_ext', '1.5.1'

Using bundler inside my gem

I wanted to use bundler inside gem I wrote. I had Gemfile and in my_gem_file.rb I have
require 'rubygems'
require 'bundler'
Bundler.setup
Bundler.require(:default)
But when I build and install my gem I get exception Bundler::GemfileNotFound: Could not locate Gemfile. Is there any solution for using bundler inside gems?
Since your gem has specified its dependencies in its .gemspec you can assume that they will be available at runtime. Don't involve Bundler.
You can, however, still make use of Bundler for the development of your gem. If you use this Gemfile:
source :rubygems
gemspec
Bundler will look in your .gemspec to determine which gems to install when you run bundle install. You can read more about that here: http://jeffkreeftmeijer.com/2010/bundler-because-your-gems-depend-on-gems-too/
Bundler is not suitable for managment depending gems in gem source. Inside a gem, just require the libraries that you need.
I disagree, Bundler is great for gem development. It helps make it easier to get started and collaborate. And keep the gemspec cleaner?
I'd eliminate the dev dependencies
Gem::Specification.new do |s|
s.add_development_dependency("mocha", ["~>0.9.8"])
of course keep the s.add_dependency("i18n", ["~>0.4.1"]) and others that your gem depends on.
You might end up with a Gemfile (as Theo shows), like this
source :rubygems
gemspec
group :test do
gem 'rails', '3.0.0'
gem 'mocha'
gem 'rspec-rails', "~>2.0.1"
end
Nice and clean, easy to read. Hope this helps.

Resources