如何查看 Ruby .gem 中的文件?

Rya*_*yan 6 ruby rubygems

一旦我安装了 Ruby gem,我就知道我可以使用gem contents <my_gem>. 但是如何在不安装 gem 的情况下查看它的内容呢?

Rya*_*yan 6

gem unpack

您可以使用该gem unpack命令将.gem文件内容提取到目录中。假设你有 some_file.gem:

gem unpack some_file.gem
Run Code Online (Sandbox Code Playgroud)

tar

如果你想在不解包的情况下列出 gem 的内容,你可以使用tar.gem文件只是.tar具有特定格式的文件)。

tar --to-stdout -xf some_file.gem data.tar.gz | tar -zt
Run Code Online (Sandbox Code Playgroud)

这是有关其工作原理的详细解释:

# Download a .gem file
$ gem fetch json_pure -v '2.0.3'
Fetching: json_pure-2.0.3.gem (100%)
Downloaded json_pure-2.0.3

# List the contents of the gem
# You can see that it contains separate files for metadata, the gem files, and a checksum
$ tar -tf json_pure-2.0.3.gem
metadata.gz
data.tar.gz
checksums.yaml.gz

# Extract just the data.tar.gz file, then unzip and list the contents of *that* file.
#   --to-stdout so we can pipe it to another command
#   -x extract
#   -f file
#
# Then:
#   -z use gunzip to decompress the .tar.gz file
#   -t list the contents of the archive
tar --to-stdout -xf json_pure-2.0.3.gem data.tar.gz | tar -zt
./tests/test_helper.rb
.gitignore
.travis.yml
CHANGES.md
Gemfile
...
Run Code Online (Sandbox Code Playgroud)

在大多数情况下,gem unpack这可能就是您想要的。上述tar命令的主要好处是它不会创建任何新目录或实际解压缩文件。如果您没有安装 ruby​​gems,它也可能很有用。