Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see files inside a Ruby .gem?

Tags:

ruby

rubygems

Once I have installed a Ruby gem I know that I can see the files inside the gem by using gem contents <my_gem>. But how can I see the contents of a gem without installing it?

like image 253
Ryan Avatar asked Apr 08 '17 17:04

Ryan


1 Answers

with gem unpack

You can use the gem unpack command to extract the contents of a .gem file into a directory. Assuming you have some_file.gem:

gem unpack some_file.gem

with tar

If you want to list the gem's contents without unpacking it, you can use tar (a .gem file is just a .tar file with a specific format).

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

Here's a longer explanation of how this works:

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

In most cases gem unpack is probably what you want. The main benefit to the tar command above is that it doesn't create any new directories or actually unpack the files. It might also be useful if you don't have rubygems installed.

like image 110
Ryan Avatar answered Oct 04 '22 01:10

Ryan