gpt4 book ai didi

ruby - 使用 bundler 时在 gemspec 中声明开发依赖项是否仍然有用?

转载 作者:数据小太阳 更新时间:2023-10-29 07:33:55 24 4
gpt4 key购买 nike

我正在开发一个新的 Ruby Gem。我熟悉使用 Bundler 来管理 gems:

source "https://rubygems.org"

gemspec

gem 'rspec-rails'

我熟悉在 gemspec 文件中指定依赖项:

Gem::Specification.new do |s|
# ...
s.add_dependency "rails", "~> 4.1.5"
end

生成的 Gemfile 提到当我准备好发布时,我应该将我的依赖声明从我的 Gemfile 移动到我的 gemspec。

# 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.

我为什么要这样做?为什么我的 gemspec 应该关心我在开发中使用的是什么 gem? development_dependency 服务于 Bundler 尚未为我服务的什么目的?

最佳答案

为了最好地回答您的问题,我们应该首先理清 Bundler 和 Rubygems 的概念。我认为一个很好的解释可以是 found here .

为什么我要[将依赖项从 Gemfile 移至 .gemspec]?

Gemfile 不仅允许您指定依赖项,还允许您指定 where the dependencies come from .当您自己也在处理依赖项并且需要指向 Git 存储库(或其他东西)时,这很有用。

完成这些依赖项的工作后,Rubygem 约定要求您将这些已发布的 gem 的依赖项声明移动到您的 .gemspec 文件中。添加一行 gemspec 告诉 Bundler 从这个传统的 Rubygems 位置读取。如果您正在开发一个 gem,并且您没有积极开发 gem 的依赖项,那么所有依赖项都应该在您的 .gemspec 中声明

为什么我的 gemspec 应该关心我在开发中使用的是什么 gem?

来自 the docs对于 add_development_dependency:

Development dependencies aren't installed by default and aren't activated when a gem is required.

此 Rspec 的一个流行示例。您通常应该将 Rspec 声明为您自己的开发依赖项,但不要强制其他人在获取您的 gem 时下载它。

关于ruby - 使用 bundler 时在 gemspec 中声明开发依赖项是否仍然有用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26184435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com