我可以调用Ruby方法来获取已安装宝石的列表吗?
我想解析gem list
的输出。有没有不同的方法来做到这一点?
答案 0 :(得分:298)
这列出了我安装的所有宝石。
gem query --local
http://guides.rubygems.org/command-reference/#gem-list
参见2.7列出所有已安装的宝石
答案 1 :(得分:49)
Gem命令现在包含在Ruby 1.9+中,是Ruby pre-1.9的标准补充。
require 'rubygems'
name = /^/i
dep = Gem::Dependency.new(name, Gem::Requirement.default)
specs = Gem.source_index.search(dep)
puts specs[0..5].map{ |s| "#{s.name} #{s.version}" }
# >> Platform 0.4.0
# >> abstract 1.0.0
# >> actionmailer 3.0.5
# >> actionpack 3.0.5
# >> activemodel 3.0.5
# >> activerecord 3.0.5
以下是获取列表的更新方式:
require 'rubygems'
def local_gems
Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }
end
因为local_gems
依赖于group_by
,它会返回宝石的散列,其中键是gem的名称,值是gem规范的数组。该值是安装的gem的实例数组,按版本号排序。
这使得可以做以下事情:
my_local_gems = local_gems()
my_local_gems['actionmailer']
# => [Gem::Specification.new do |s|
# s.authors = ["David Heinemeier Hansson"]
# s.date = Time.utc(2013, 12, 3)
# s.dependencies = [Gem::Dependency.new("actionpack",
# Gem::Requirement.new(["= 4.0.2"]),
# :runtime),
# Gem::Dependency.new("mail",
# Gem::Requirement.new(["~> 2.5.4"]),
# :runtime)]
# s.description = "Email on Rails. Compose, deliver, receive, and test emails using the familiar controller/view pattern. First-class support for multipart email and attachments."
# s.email = "david@loudthinking.com"
# s.homepage = "http://www.rubyonrails.org"
# s.licenses = ["MIT"]
# s.name = "actionmailer"
# s.require_paths = ["lib"]
# s.required_ruby_version = Gem::Requirement.new([">= 1.9.3"])
# s.requirements = ["none"]
# s.rubygems_version = "2.0.14"
# s.specification_version = 4
# s.summary = "Email composition, delivery, and receiving framework (part of Rails)."
# s.version = Gem::Version.new("4.0.2")
# end]
和
puts my_local_gems.map{ |name, specs|
[
name,
specs.map{ |spec| spec.version.to_s }.join(',')
].join(' ')
}
# >> actionmailer 4.0.2
...
# >> arel 4.0.1,5.0.0
...
# >> ZenTest 4.9.5
# >> zucker 13.1
最后一个示例类似于gem query --local
命令行,只有您可以访问特定gem规范的所有信息。
答案 2 :(得分:25)
两个
gem query --local
和
ruby -S gem list --local
列出69个条目
虽然
ruby -e 'puts Gem::Specification.all_names'
给了我82
我用wc -l
来获取数字。不确定这是否是正确的检查方式。试图将输出重定向到文本文件和差异,但没有帮助 - 将需要逐个手动比较。
答案 3 :(得分:9)
多年来一直有这种方法:
ruby -e 'puts Gem::Specification.all_names'
答案 4 :(得分:5)
Gem::Specification.map {|a| a.name}
但是,如果您的应用使用Bundler,则只会返回相关本地宝石的列表。要全部安装:
def all_installed_gems
Gem::Specification.all = nil
all = Gem::Specification.map{|a| a.name}
Gem::Specification.reset
all
end
答案 5 :(得分:3)
更现代的版本是使用类似于以下内容的东西......
require 'rubygems'
puts Gem::Specification.all().map{|g| [g.name, g.version.to_s].join('-') }
注意:与Evgeny的答案的第一部分非常相似......但由于页面格式化,很容易错过。
答案 6 :(得分:3)
使用此代码(在控制台模式下):
Gem::Specification.all_names
答案 7 :(得分:2)
在终端尝试:
ruby -S gem list --local
答案 8 :(得分:2)
这是一个非常好的单线版,可以打印所有宝石及其版本,主页和描述:
<!DOCTYPE html>
<html>
<head>
<title>Satyam</title>
<style>
ul,li{background-image:-webkit-linear-gradient(#0CE840,#EFFFE5);
display:inline-block;}
*{margin=0px;padding=0px;border:3px solid #000;background-attachment:fixed;
background-image: url("http://cdn.sploder.com/images/hp3/hp_multiplayer_sploderheads2.gif");
background-size:cover;}
#main{font-size:100px;box-sizing:border-
box;position:relative;bottom:90px;background-image:-webkit-linear-
gradient(#FFE259,#EFFFE5)}</style>
</head>
<body>
<h2 align="center" id="main">Gamerz!</h2>
<ul><h4>
<li>Home
<li>Courses
<li>Support
<li>Contact us</h4>
</ul>
</html>
答案 9 :(得分:1)
也许你可以从gems目录中获取文件(gems)?
gemsdir = "gems directory"
gems = Dir.new(gemsdir).entries
答案 10 :(得分:1)
从您的调试器类型$LOAD_PATH
中获取您的宝石列表。如果您没有调试器,请安装pry:
gem install pry
pry
Pry(main)> $LOAD_PATH
这将输出已安装宝石的数组。