Gem.bin_path('cucumber', 'cucumber')
Will return the binary/executable's path. It seems there is no such function to return the library path. Which in this case would, ideally, return:
将返回二进制/可执行文件的路径。似乎没有返回库路径的函数。在这种情况下,理想情况下,返回:
/home/hedge/.rvm/gems/ruby-1.9.2-p136@bbb-bdd-meta-bdd/gems/cucumber-0.10.0/lib
Have I missed something or is there a simple/one method way to get this information?
我是否漏掉了什么,或者有一个简单的方法来获取这些信息?
Updated: No CLI or non-stdlib suggestions please.
更新:无CLI或非stdlib建议。
4 个解决方案
#1
73
The problem with the checked answer is that you must "require" the rubygem or it won't work. Often this is undesireable because if you're working with an executable gem, you don't want to "require" it or you'll get a bunch of warnings.
检查过的答案的问题是,你必须“要求”rubygem,否则它就不会工作。这通常是不可取的,因为如果您使用可执行gem,您不希望“要求”它,否则您将得到大量警告。
This is a universal solution for executables and libs:
这是可执行程序和libs的通用解决方案:
spec = Gem::Specification.find_by_name("cucumber")
gem_root = spec.gem_dir
gem_lib = gem_root + "/lib"
If you want to get really technical, there isn't just one lib directory. The gemspec has a "require_paths" array of all the directorys to search (added to $LOAD_PATH). So, if you want an array of the require_paths, use this:
如果您想要获得真正的技术,那么并不只有一个lib目录。gemspec有一个“require_path”数组,所有的directorys都可以搜索(添加到$LOAD_PATH)。因此,如果您想要一个require_paths数组,请使用:
gem_lib = gem_root + "/" + spec.require_paths[0]
No need for bundler.
不需要打包机。
#2
23
After the gem has been loaded with require, you find the lib path using Gem.loaded_specs as follows:
在gem加载了require之后,您可以使用gem找到lib路径。loaded_specs如下:
require 'rubygems'
require 'cucumber'
gem_root = Gem.loaded_specs['cucumber'].full_gem_path
gem_lib = File.join(gem_root, 'lib')
#3
15
I'm not sure why you're doing this, but if you're on the command line, use gem env
.
我不确定为什么要这样做,但是如果您在命令行上,请使用gem env。
#4
4
Try using bundle show cucumber
.
试着用打包展示黄瓜。
Which, from looking at the source of bundler does something like:
从看本德勒的资料可以看出:
spec = Bundler.load.specs.find{|s| s.name == name }
spec.full_gem_path
You are using bundler, right?
你用的是bundler,对吧?
#1
73
The problem with the checked answer is that you must "require" the rubygem or it won't work. Often this is undesireable because if you're working with an executable gem, you don't want to "require" it or you'll get a bunch of warnings.
检查过的答案的问题是,你必须“要求”rubygem,否则它就不会工作。这通常是不可取的,因为如果您使用可执行gem,您不希望“要求”它,否则您将得到大量警告。
This is a universal solution for executables and libs:
这是可执行程序和libs的通用解决方案:
spec = Gem::Specification.find_by_name("cucumber")
gem_root = spec.gem_dir
gem_lib = gem_root + "/lib"
If you want to get really technical, there isn't just one lib directory. The gemspec has a "require_paths" array of all the directorys to search (added to $LOAD_PATH). So, if you want an array of the require_paths, use this:
如果您想要获得真正的技术,那么并不只有一个lib目录。gemspec有一个“require_path”数组,所有的directorys都可以搜索(添加到$LOAD_PATH)。因此,如果您想要一个require_paths数组,请使用:
gem_lib = gem_root + "/" + spec.require_paths[0]
No need for bundler.
不需要打包机。
#2
23
After the gem has been loaded with require, you find the lib path using Gem.loaded_specs as follows:
在gem加载了require之后,您可以使用gem找到lib路径。loaded_specs如下:
require 'rubygems'
require 'cucumber'
gem_root = Gem.loaded_specs['cucumber'].full_gem_path
gem_lib = File.join(gem_root, 'lib')
#3
15
I'm not sure why you're doing this, but if you're on the command line, use gem env
.
我不确定为什么要这样做,但是如果您在命令行上,请使用gem env。
#4
4
Try using bundle show cucumber
.
试着用打包展示黄瓜。
Which, from looking at the source of bundler does something like:
从看本德勒的资料可以看出:
spec = Bundler.load.specs.find{|s| s.name == name }
spec.full_gem_path
You are using bundler, right?
你用的是bundler,对吧?