简体   繁体   English

我如何需要宝石?

[英]How do I require gems?

I'm new to Ruby and I'm writing a pure Ruby script, not Rails. 我是Ruby的新手,我正在编写一个纯Ruby脚本,而不是Rails。

This script is trivial: 这个脚本很简单:

require 'progressbar'

bar = ProgressBar.new("Example progress", 50)
total = 0
until total >= 50
  sleep(rand(2)/2.0)
  increment = (rand(6) + 3)
  bar.inc(increment)
  total += increment
end

When I run it, I get this: 运行它时,我得到以下信息:

./progressbar.rb:3: uninitialized constant ProgressBar (NameError)
from progressbar.rb:1:in `require'
from progressbar.rb:1

The gem is installed. 该宝石已安装。 What am I doing wrong? 我究竟做错了什么?

Judging by the backtrace, I think ruby is actually trying to load your progressbar.rb rather than the one from the gem. 从回溯的角度来看,我认为红宝石实际上是在尝试加载您的progressbar.rb而不是gem中的那个。

Prior to 1.9 you should also do require 'rubygems' so that the rubygems library itself is loaded 在1.9之前,您还应该require 'rubygems'以便加载rubygems库本身

If you're on Ruby 1.8.x, try to rename your source file to progressbar_test.rb and run it instead. 如果您使用的是Ruby 1.8.x,请尝试将源文件重命名为progressbar_test.rb并运行它。 Chances are that's all there's to it, since your require statement tries to load your own sourcefile instead of the one in the gem. 因为您的require语句尝试加载自己的源文件,而不是gem中的源文件,所以这就是全部。

Assuming you are on Ruby 1.8, I would rename the file you've in the question from progressbar.rb to pg_test.rb or anything other than progressbar.rb . 假设您使用的是Ruby 1.8,则将问题中的文件从progressbar.rb重命名为pg_test.rbprogressbar.rb以外的其他任何文件。

require 'rubygems'
require 'progressbar'

bar = ProgressBar.new("Example progress", 50)
total = 0
until total >= 50
  sleep(rand(2)/2.0)
  increment = (rand(6) + 3)
  bar.inc(increment)
  total += increment
end

In the shell: 在外壳中:

$ ruby ./pg_test.rb

Bundler is by far the best solution for managing dependencies in ruby apps. 到目前为止, Bundler是管理ruby应用程序中依赖项的最佳解决方案。

$ gem install bundler
$ bundle gem <gem-name>

You'll get the standard file structure for a ruby gem library that looks like this: 您将获得一个如下所示的ruby gem库的标准文件结构:

my_awesome_gem
  Gemfile
  my_awesome_gem.gemspec
  Rakefile
  lib
    my_awesome_gem.rb
    my_awesome_gem
      version.rb

Open up your Gemfile and add your dependencies like this 打开您的Gemfile并添加您的依赖项,如下所示

gem 'progressbar'

Then in lib/my_awesome_gem.rb do this: 然后在lib / my_awesome_gem.rb中执行以下操作:

require 'bundler/setup' #initialize bundler, which works its magic on your load path
require 'progressbar' #require whatever gems (make sure they're listed in your Gemfile)

bar = ProgressBar.new("Example progress", 50)
total = 0
until total >= 50
  sleep(rand(2)/2.0)
  increment = (rand(6) + 3)
  bar.inc(increment)
  total += increment
end

Finally, to run your script: 最后,运行脚本:

$ ruby lib/my_awesome_gem.rb

Bunder is definitely the best way to do this. Bunder绝对是做到这一点的最佳方法。 You won't have issues with the differences in ruby versions and you know your script will fit in with the standards and best practices of the ruby community. 您将不会遇到红宝石版本差异的问题,并且您知道您的脚本将符合红宝石社区的标准和最佳实践。 I highly recommend using bundler from the beginning. 我强烈建议从一开始就使用捆绑程序。 This also helps when you start adding test because you're already setup to start require ing any of the gems you've listed as dependencies in your tests as well. 因为你已经完成,可以开始,当你开始添加测试这也有助于require荷兰国际集团任何你在测试中列出的依赖,以及宝石的。

Plus, if your script gets to the point that you'd like to distribute it, it's already set up to publish to rubygems.org. 另外,如果您的脚本到了要分发的地步,那么它已经设置为发布到rubygems.org。

Check out this blog post by yehuda katz for more on that. 查阅yehuda katz的博客文章 ,了解更多信息。

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

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