Using bundler inside my gem - ruby

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.

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'

Install requirements automatically

I wrote a ruby gem that requires another gem -> 'curl'.
How can i make it happen that 'curl' my required gem is getting installed along with my own when i run:
gem install MyGem-1.0.0.gem
The RubyGems specifications (the .gemspec file) allows you to list a gem as a dependency of your gem. This will cause RubyGems to install the dependency (in your case curl) automatically when your gem is installed.
Gem::Specification.new do |spec|
# ...
spec.add_runtime_dependency 'curl', '~> 1.1'
end
If you aren't using bundler, you just need to add 'curl' gem as a runtime dependency in your gemspec file.
spec.add_runtime_dependency 'example', '~> 1.1', '>= 1.1.4'
Detailed reference: http://guides.rubygems.org/specification-reference/#add_runtime_dependency
Here is good guide for creating gem with bundler

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.

How to search for useful ruby gems

What are good sites to look for useful ruby gems?
Agile Web Development lists plugins (though not rubygems, I'm not sure why), and allows people to give ratings for them.
Ruby Toolbox lists gems by category and compares how popular they are.
Rubygems has a search box.
Stack Overflow has a question on most useful rails plugins and rubygems.
Github is where most gems source code is hosted; they have a search facility, and you can search by language as well.
I have compiled a list of usefull gems for development machine. You can check the list here with detailed explaination
gem "better_errors"
gem "binding_of_caller"
gem 'annotate'
gem 'bullet'
gem 'debugger'
gem 'flay'
gem 'hirb'
gem 'localtunnel'
gem 'lol_dba'
gem 'mailcatcher'
gem 'meta_request','0.2.1'
gem 'pry'
gem 'pry-doc'
gem 'quiet_assets'
gem 'rack-mini-profiler'
gem 'railroady'
gem 'rails-footnotes', '>= 3.7.5.rc4'
gem 'rails_best_practices'
gem 'reek'
gem 'request-log-analyzer'
gem 'smusher'
gem 'zeus' # don't add this in your gemfile.

Resources