I'm writing a ruby gem which has a dependency on another gem.
Eg. I'm writing a gem called "ABC" which has a dependency on "XYZ". How to make my gem install "XYZ" automatically when I try to install "ABC" ?
I'm writing a gem called "ABC" which has a dependency on "XYZ". How to make my gem install "XYZ" automatically when I try to install "ABC" ?
You shouldn't try to make your gem install the dependencies automatically. That's literally what RubyGems is for. RubyGems resolves dependencies automatically, there is no need for your gem to do that.
Just define required dependency in your .gemspec file:
https://guides.rubygems.org/specification-reference/#add_runtime_dependency
spec.add_runtime_dependency 'example', '~> 1.1', '>= 1.1.4'
and they will be installed when your gem is being installed (or just used if the dependency is already there).
There's also a way to install the dev dependencies: https://guides.rubygems.org/specification-reference/#add_development_dependency
spec.add_development_dependency 'example', '~> 1.1', '>= 1.1.4'
for those gems that are required for developing (like rspec) but not needed for the final product to work.
Related
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.
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
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
I am using rvm, ruby 2.0.0 and bundler.
My Gemfile looks like this:
source 'https://rubygems.org'
gem 'logger'
gem 'mygem', :path => '.'
bundle installs both of them gems. bundle show shows logger is installed in ~/.rvm/gems/ruby-2.0.0-p247/gems, but mygem is installed in the path where the gem is located.
Is there any way to get bundle to install the local gem into rvm's gems directory?
No, Bundler treats path gems differently and does not install them to your GEM_PATH. This is so that you don't need to reinstall as you make changes.
It is not normal or necessary for a gem to point to itself or its runtime dependencies in its Gemfile. You might want to add gemspec to do this automatically. See http://bundler.io/v1.3/rubygems.html
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.