简体   繁体   中英

How do I require gems?

I'm new to Ruby and I'm writing a pure Ruby script, not 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.

Prior to 1.9 you should also do require 'rubygems' so that the rubygems library itself is loaded

If you're on Ruby 1.8.x, try to rename your source file to progressbar_test.rb and run it instead. 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.

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 .

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.

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

You'll get the standard file structure for a ruby gem library that looks like this:

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

gem 'progressbar'

Then in lib/my_awesome_gem.rb do this:

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

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.

Check out this blog post by yehuda katz for more on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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