class Gem::Package

使用 Gem::Package 的示例

根据 Gem::Specification 构建 .gem 文件。.gem 文件是一个 tarball,其中包含 data.tar.gz、metadata.gz、checksums.yaml.gz 和可能的签名。

require 'rubygems'
require 'rubygems/package'

spec = Gem::Specification.new do |s|
  s.summary = "Ruby based make-like utility."
  s.name = 'rake'
  s.version = PKG_VERSION
  s.requirements << 'none'
  s.files = PKG_FILES
  s.description = <<-EOF
Rake is a Make-like program implemented in Ruby. Tasks
and dependencies are specified in standard Ruby syntax.
  EOF
end

Gem::Package.build spec

读取 .gem 文件。

require 'rubygems'
require 'rubygems/package'

the_gem = Gem::Package.new(path_to_dot_gem)
the_gem.contents # get the files in the gem
the_gem.extract_files destination_directory # extract the gem into a directory
the_gem.spec # get the spec out of the gem
the_gem.verify # check the gem is OK (contains valid gem specification, contains a not corrupt contents archive)

files 是 .gem tar 文件中的文件,而不是 gem 中的 Ruby 文件。extract_filescontents 会自动调用 verify