Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

ruby - I have a gem installed but require 'gemname' does not work. Why?

The question I'm really asking is why require does not take the name of the gem. Also, In the case that it doesn't, what's the easiest way to find the secret incantation to require the damn thing!?

As an example if I have memcache-client installed then I have to require it using

require 'rubygems'
require 'memcache'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My system also doesn't seem to know about RubyGems' existence - unless I tell it to. The 'require' command gets overwritten by RubyGems so it can load gems, but unless you have RubyGems already required it has no idea how to do that. So if you're writing your own, you can do:

require 'rubygems'
require 'gem-name-here'

If you're running someone else's code, you can do it on the command line with:

ruby -r rubygems script.rb

Also, there's an environment variable Ruby uses to determine what it should load up on startup:

export RUBYOPT=rubygems

(from http://www.rubygems.org/read/chapter/3. The environment variable thing was pointed out to me by Orion Edwards)

(If "require 'rubygems' doesn't work for you, however, this advice is of limited help :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...