已安装的宝石列表? [英] List of installed gems?

查看:12
本文介绍了已安装的宝石列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有我可以调用的 Ruby 方法来获取已安装 gem 的列表?

Is there a Ruby method I can call to get the list of installed gems?

我想解析gem list的输出.有没有其他方法可以做到这一点?

I want to parse the output of gem list. Is there a different way to do this?

推荐答案

Gem 命令现在包含在 Ruby 1.9+ 中,并且是 Ruby pre-1.9 的标准添加.

The Gem command is included with Ruby 1.9+ now, and is a standard addition to 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

<小时>

这是获取列表的更新方法:


Here's an updated way to get a list:

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 的hash,其中key 是gem 的名称,value 是gem 规范的数组.该值是已安装的 gem 实例的数组,按版本号排序.

Because local_gems relies on group_by, it returns a hash of the gems, where the key is the gem's name, and the value is an array of the gem specifications. The value is an array of the instances of that gem that is installed, sorted by the version number.

这样就可以执行以下操作:

That makes it possible to do things like:

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 规范的所有信息.

The last example is similar to the gem query --local command-line, only you have access to all the information for a particular gem's specification.

这篇关于已安装的宝石列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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