【问题标题】:Is the net/http gem not in the default library for Ruby?net/http gem 不在 Ruby 的默认库中吗?
【发布时间】:2021-06-12 00:56:30
【问题描述】:

我想通过键入“bundle install”来创建 Gemfile.lock,但我的本地机器找不到 gem net/http。我尝试输入 'bundle update net/http' & 'bundle --full-index' & 'gem install bundler' 但当我再次尝试 'bundle install' 时,我不断收到此错误:

在 ruby​​gems 存储库 https://rubygems.org/ 中找不到 gem 'net/http' 或安装在本地。源不包含任何版本的 'net/http'

我的 Gemfile 类似于以下内容:

source "https://rubygems.org"

gem 'open-uri'
gem 'nokogiri'
gem 'net/http'
gem 'pry'

此问题的其他解决方案建议删除 gem net/http 的行,因为 net/http 是 Ruby 的默认库的一部分......但是当我这样做时,一切都很好,我可以创建一个 Gemfile.lock输入“捆绑安装”,但当我运行我的代码时,我收到以下错误:

Traceback(最近一次调用最后一次): run.rb:4:in `': 未初始化的常量 Net (NameError) 你的意思是?设置

这里指的代码行是

response = Net::HTTP.get(url)

我正在运行 Ruby 版本 2.6.1

【问题讨论】:

  • require 'net/http'了吗?

标签: ruby rubygems net-http


【解决方案1】:

宝石的名称是net-http。当您在 Google 上搜索“net/http”时,它是第一个点击。

但是,在 Ruby 2.6.1 中,net/http is still part of the standard library 不是 gem。 Net/http 仅从 Ruby 3.0 的标准库中删除。

【讨论】:

    【解决方案2】:

    有两种不同的标准宝石:

    • 默认 gems:这些 gems 是 Ruby 的一部分,您可以随时直接要求它们。您无法删除它们。它们由 Ruby 核心维护。

    • 捆绑的 gem:捆绑的 gem 的行为类似于普通的 gem,但它们会在您安装 Ruby 时自动安装。它们可以卸载并在 Ruby 核心之外进行维护。

    您的问题是 gem 名称错误('net-http' 而不是 'net/http',您可以运行 gem search ^net 来找出以 'net' 开头的远程 gem)。

    如果 gem 是 'default',则不需要在 Gemfile 中声明它。

    您可以在以下位置检查标准默认宝石:https://stdgems.org/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 2015-01-09
      • 2011-12-22
      • 1970-01-01
      相关资源
      最近更新 更多