对于使用 Gemfile 的 Ruby 应用程序,无法通过 Bundler.setup 要求 pry

bod*_*tva 0 ruby bundler gemfile pry

我正在编写一个简单的 ruby​​ 应用程序(基本上使用 PORO 的)。我已经向它添加了一个 Gemfile,我正在尝试通过 Gemfile 要求使用 pry gem(这对于在调试时添加断点很有用,随着应用程序的增长),但我无法要求使用该 gem Bundler.setup,一切正常与Bundler.require.

例如Bundler.require,出于本博客中所述的原因,我试图避免使用。

在Employee.rb文件中,我有以下代码 -

# require 'bundler'
# require 'bundler/setup'
# Bundler.setup(:default, :test, :development)

Bundler.require(:default, :development, :test)

def total_score(scores)
  binding.pry #added on purpose , just to see if the app stops at this breakpoint
  scores.inject(:+)
end
Run Code Online (Sandbox Code Playgroud)

当我使用 Bundler.setup(取消注释上面注释的行)而不是 Bundler.require 时,我rspec spec .从我的应用程序根目录给出的命令出现以下错误 -

 Failure/Error: expect(total_score(scores)).to eq(16)
 NoMethodError:
   undefined method `pry' for #<Binding:0x007fbda22b79d0>
 # ./Employee.rb:9:in `total_score'
 # ./spec/employee_spec.rb:5:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

另外,还有一点要注意,根据bundler docs,我什至尝试做一个require rubygems,但即使这样也无济于事。理想情况下,require rubygems由于此链接中提到的原因,我希望避免使用。

我如何让我的应用程序使用 Bundler.setup 来要求 pry gem,而无需执行require rubygems并排也无需使用Bunder.require?

Rya*_*igg 5

Bundler.setup将简单地将 gem 添加到加载路径,而Bundler.require将添加到加载路径和要求。

我建议使用,Bundler.setup以便您的代码启动得更快。然后,当您需要使用 pry 时,请执行以下操作:

require 'pry'
Run Code Online (Sandbox Code Playgroud)