我安装了一个 gem,但 require 'gemname' 不起作用.为什么? [英] I have a gem installed but require 'gemname' does not work. Why?

查看:12
本文介绍了我安装了一个 gem,但 require 'gemname' 不起作用.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真正要问的问题是,为什么 require 不采用 gem 的名称.另外,如果没有,最简单的方法是找到需要该死的东西的秘密咒语!?

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!?

例如,如果我安装了 memcache-client,那么我必须使用它来要求它

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

require 'rubygems'
require 'memcache'

推荐答案

您需要包含的文件没有标准.但是,您可以尝试并使用一些常用的约定:

There is no standard for what the file you need to include is. However there are some commonly followed conventions that you can can follow try and make use of:

  • 通常文件被称为相同名称为宝石.所以需要mygem将工作.
  • 文件通常是库中唯一的 .rb 文件gem 的子目录,所以如果你可以获得宝石的名称(也许你正在迭代2.1 之前的轨道中的供应商/宝石项目),然后你可以检查#{gemname}/lib 用于 .rb 文件,以及如果只有一个,它很漂亮这是一个很好的选择
  • Often the file is called the same name as the gem. So require mygem will work.
  • Often the file is the only .rb file in the lib subdirectory of the gem, So if you can get the name of the gem (maybe you are itterating through vendor/gems in a pre 2.1 rails project), then you can inspect #{gemname}/lib for .rb files, and if there is only one, its a pretty good bet that is the one to require

如果所有这些都有效,那么您所能做的就是查看 gem 的目录(您可以通过运行 gem environment | grep INSTALLATION | awk '{print $4}' 并查看lib 目录,您可能需要阅读这些文件并希望有注释说明该做什么

If all of that works, then all you can do is look into the gem's directory (which you can find by running gem environment | grep INSTALLATION | awk '{print $4}' and looking in the lib directory, You will probably need to read the files and hope there is a comment explaining what to do

这篇关于我安装了一个 gem,但 require 'gemname' 不起作用.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆