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

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

问题描述



我想解析的输出gem list 。有没有不同的方法来做到这一点?

解决方案

Gem命令现在包含在Ruby 1.9+中, to Ruby pre-1.9。

  require'rubygems'

name = / ^ / i
dep = Gem :: Dependency.new(name,Gem :: Requirement.default)
specs = Gem.source_index.search(dep)
放置specs [0..5] .map {| S | #{s.name}#{s.version}}
#>>平台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 ,它返回一个gems散列,其中key是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(邮件,
#Gem :: Requirement.new([〜> 2.5.4]),
#:runtime)]
#s.description =Email on Rails。使用熟悉的控制器/视图模式撰写,发送,接收和测试电子邮件。为多部分电子邮件和附件提供一流的支持。
#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 = 电子邮件的撰写,发送和接收框架(Rails的一部分)。
#s.version = Gem :: Version.new(4.0.2)
#end]

和:

  puts my_local_gems.map {| name,specs | 
[
名称,
specs.map {| spec | (',')
] .join('')
}
#>> actionmailer 4.0.2
...
#>> arel 4.0.1,5.0.0
...
#>> ZenTest 4.9.5
#>> zucker 13.1

最后一个例子与 gem query --local 命令行,只有您有权访问特定宝石规范的所有信息。


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

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

解决方案

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

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]

And:

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

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天全站免登陆