简体   繁体   English

Ruby - 无法使用本地安装的gem

[英]Ruby - Cannot use locally installed gem

I've written a simple PasswordGenerator gem that I have at ~/workspace/gems/password_generator and have an app at ~/workspace/rubysamples/app where I want to use it. 我在~/workspace/gems/password_generator编写了一个简单的PasswordGenerator gem,并在~/workspace/rubysamples/app中有一个应用~/workspace/rubysamples/app ,我想在其中使用它。 I have a Gemfile , the content of it is this: 我有一个Gemfile ,它的内容是这样的:

gem 'password_generator', path: '~/workspace/gems/password_generator'

I installed it locally, like this: 我在本地安装它,如下所示:

bundle install --local
Resolving dependencies...
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `~/workspace/gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

It looks like it's installed locally: 它看起来像是在本地安装的:

bundle info password_generator
  * password_generator (0.1.0)
    Summary: Simple password generator
    Homepage: https://github.com/jedrekdomanski/password_generator
    Path: /home/jedrek/workspace/gems/password_generator

When I try to use it 当我尝试使用它

~/workspace/rubysamples/app/password_reset.rb 〜/工作区/ ruby​​samples /应用程序/ password_reset.rb

PasswordGenerator.generate

I get an error 我收到一个错误

uninitialized constant PasswordGenerator (NameError)

What am I doing wrong? 我究竟做错了什么? Am I missing anything? 我错过了什么吗?

Here's my gem repo: https://github.com/jedrekdomanski/password_generator 这是我的宝石回购: https//github.com/jedrekdomanski/password_generator

I also tried pointing to my repo and branch in the Gemfile 我也尝试指向我的repo并在Gemfile中分支

gem 'password_generator', git: 'git@github.com:jedrekdomanski/password_generator.git', branch: 'master'

but I get the same error message uninitialized constant PasswordGenerator (NameError) 但是我得到了相同的错误信息uninitialized constant PasswordGenerator (NameError)

There are potentially two issues. 可能存在两个问题。 The first is how you are starting Ruby and the second is how you are requiring your module. 第一个是你如何启动Ruby,第二个是你如何需要你的模块。

First, if you are starting Ruby by running ruby password_reset.rb then you are ignoring the Gemfile . 首先,如果你通过运行ruby password_reset.rb启动Ruby,那么你忽略了Gemfile The Gemfile is only used when you're using bundler , so you want to make sure you are starting Ruby by running bundle exec ruby password_reset.rb . Gemfile仅在您使用bundler时使用,因此您希望通过运行bundle exec ruby password_reset.rb来确保启动Ruby。 This causes bundler to read your Gemfile and execute Ruby in that context. 这会导致bundler读取您的Gemfile并在该上下文中执行Ruby。

Second, you're not properly including your module in your Ruby file. 其次,你没有在你的Ruby文件中正确包含你的模块。 Just because you've added the gem to your Gemfile and started Ruby using bundler doesn't mean that the Ruby process knows you intend to use that gem's module; 只是因为你已经将gem添加到你的Gemfile并使用bundler启动Ruby并不意味着Ruby进程知道你打算使用那个gem的模块; it just makes the module available for use. 它只是使模块可用 You might wonder, "Why don't I have to do that in Rails?" 您可能想知道,“为什么我不必在Rails中这样做?” Because Rails does that for you automatically via config/application.rb . 因为Rails通过config/application.rb自动为您完成

Given these two issues, the correct way to accomplish your goal is to configure your app as follows: 鉴于这两个问题,实现目标的正确方法是按如下方式配置您的应用:

First, create your Gemfile : 首先,创建您的Gemfile

# Gemfile
gem 'password_generator', path: '~/workspace/gems/password_generator'

Second, create your password_reset.rb file: 其次,创建您的password_reset.rb文件:

# password_reset.rb
# Manually require any libraries that this app will use, even if defined in Gemfile
require 'password_generator'
# Call `puts` so something is printed to the console when this app runs
puts PasswordGenerator.generate

Third, run bundle install to ensure your Gemfile is properly formatted and to generate your Gemfile.lock : 第三,运行bundle install以确保Gemfile格式正确并生成Gemfile.lock

⇒  bundle install
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `../../gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Fourth, run bundle exec ruby password_reset.rb and observe the output: 四,运行bundle exec ruby password_reset.rb并观察输出:

⇒  bundle exec ruby password_reset.rb
kpiDfyTxtdAsKmYuZqmK

Everything works because: 一切正常,因为:

  1. Ruby is started with Bundler Ruby是从Bundler开始的
  2. Bundler reads your Gemfile and makes the gems available to Ruby Bundler读取您的Gemfile并使Ruby可用于宝石
  3. Your app requires the module from the gem before attempting to use the module 在尝试使用该模块之前,您的应用程序需要gem中的模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM