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

时间:2017-04-08 17:17:58

标签: ruby rubygems

一旦我安装了Ruby gem,我知道我可以使用gem contents <my_gem>查看gem中的文件。但是如何在不安装宝石的情况下看到宝石的内容?

1 个答案:

答案 0 :(得分:3)

gem unpack

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

gem unpack some_file.gem

tar

如果你想列出宝石的内容而不用解压缩它,你可以使用tar.gem文件只是.tar具有特定格式的文件。)

tar --to-stdout -xf some_file.gem data.tar.gz | tar -zt

以下是对其工作原理的更长解释:

# 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
...

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