Difference Between Add_Dependency and Add_Runtime_Dependency

Difference between add_dependency and add_runtime_dependency?

They are the same. add_dependency is just an alias for add_runtime_dependency.

How to work with gemspec add_runtime_dependency and `bundle install`

from bundler's documentation

Because we have the gemspec method call in our Gemfile, Bundler will automatically add this gem to a group called “development” which then we can reference any time we want to load these gems with the following line:

Bundler.require(:default, :development)

in your case, if you wish to install all rubygems that are not for development, then try

bundle install --without development

for future bundler version, you can configure it locally (or globally)

bundle config set --local without 'development'

to make it all work, verify that you have a Gemfile in your project, which will look like

# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

rubygems / add runtime dependency only for test (or any given env)

If your gem has a runtime dependency it should be listed in the gemspec as that is
what allows Bundler to do dependency resolution to see if your gem is compatible with the other gems in the Gemfile.

It is up to the end user to place your gem in a group in the Gemfile. If they place it in the :test group it will only be loaded in the test environment. If they did not read the readme and placed it in the main group then its not your problem.

Gem::Specification.new do |s|
# ...
s.add_dependency 'factory_bot', version
# ...
end

Note that you can also list development dependencies in your gemspec.

Gem::Specification.new do |s|
# ...
s.add_development_dependency 'rubocop', '~> 0.44.1'
end

These dependencies will be used when developing/testing the gem itself but are not "passed on" when you install the gem via bundler.

Is it a better practice to let the gem raise if the concerned
dependency is not added by the user in its project ?

No. Ruby already has a good dependency resolver (Bundler). Use it.

Rails 3.1 Engines: Difference of my_engine.gemspec, add_dependency, add_development_dependency, and Gemfile

Here is what I found out so far.

Within an engine (created using rails plugin new my_engine) you have to specify the needed gems only in the my_engine.gemspec file, as they are then referenced from the test/dummy/Gemfile using gemspec.

Here's the generated test/dummy/Gemfile content:

source "http://rubygems.org"

# Declare your gem's dependencies in simple_view_helpers.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

# jquery-rails is used by the dummy application
gem "jquery-rails"

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use debugger
# gem 'debugger'

What the line gem "jquery-rails" is doing here I really don't know, seems to completely contradict what's proposed in the comments. On the other hand, when I'm trying to use the SLIM gem (instead of ERB) in my test/dummy application, it seems I do have to specify it in the Gemfile, otherwise it won't work. Still a bit confusing, this stuff...

How to add dependency of a local gem to a rails plugin/engine, in .gemspec file

It's likely not possible to add local dependencies because other users will not be able to access the gem as it is local dependent and hence of no use after publish. Instead of that, Add remote dependency in your own plugin's gemspec.

Steps -

1) Open gemspec file of your own plugin in vendor/plugins/my_plugin/ and add before the keyword end:

s.add_dependency('will_paginate', '~> 3.0.pre2')

(In this example I have used will_paginate required dependency of my_plugin)

2) Now go in your rails app and edit Gemfile, add:

gem 'my_plugin', path: 'vendor/plugins/my_plugin'

3) Now in rails app root do:

bundle install

And dependency of my_plugin (will_paginate in this case) is installed.

What is reccomended practice for version restrictions in rubygem add_dependency?

If you know that your gem works with rubyzip 1.9, then there's really no need to force people to use >=2.0 with it.

Sure, updating dependencies would be a good idea for your library-user to do, but it's not your job to be the "update-your-software-police"!

Specifying that the version must be < 3 is generally advisable (although not consistently done by developers), as there's a reasonable risk that a major dependency version bump will be incompatible with this version of your code.

So, as a compromise, you could do:

spec.add_runtime_dependency 'rubyzip', '>=1.9', '<3'

See the documentation for valid syntax examples.

What kind of dependencies must be in Gemfile and in gemspec?

If you are developing a new gem, then you'll want to declare all of your production-ready gems in the .gemspec using add_dependency.

As for the Gemfile itself, as the comment states, it is used for adding dependencies which are still in development (i.e. not released). For example, if you want to use the latest edge version of Rails, you'd have to specify that dependency with the git or github option (e.g. gem "rails", github: "rails/rails"). These options are only available in the Gemfile, not the .gemspec.

In general you want to always put your dependencies in the .gemspec and only use the Gemfile if you need to.

In a ruby .gemspec file, how do I specify multiple versions of a dependency?

Accordly to the documentation, if you want to have all version between 3 and 4, you can do this :

s.add_dependency "activeresource", ">= 3.0", "< 5.0"

The specifier accepted are : >=, ~>, <=, >, <.

Gemspec: How can I specify dependencies which don't have to be auto-required?

Gems specified in an engines gemspec file already do need to be explicitly required, by default. From the official documentation - Note that if you want to immediately require dependencies when the engine is required, you should require them before the engine's initialization. In your case, you should be able to get by with something like gem.add_dependency 'uuidtools', '2.1.3' in your gemspec file, and requires in the relevant locations.



Related Topics



Leave a reply



Submit