Viewing a Gem's Source Code

RubyRubygems

Ruby Problem Overview


Ruby dabbler/newbie here who's not familiar with the ecosystem, so apologies if this is one of those super duh questions.

Is there a way to view all the files and/or source code installed by a gem? That is, I just ran

$ gem install sass

And the sass gem is now a part of my local system

$ gem list --local
...
sass (3.1.16, 3.1.2)
...

I want to know what the gem install command put on my system. Is there a command I can run to see all the files installed by the gem?

After some googling, man gem and gem help commands, I discovered the contents command.

$ gem contents sass

However, when I run this command with the aforementioned sass gem, I get the following results

.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/engine_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/functions_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/extend_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/logger_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/css2sass_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/conversion_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/script_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/util/subset_map_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/util/multibyte_string_scanner_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/callbacks_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/importer_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/scss/css_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/scss/scss_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/scss/rx_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/util_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/script_conversion_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/less_conversion_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/cache_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/plugin_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/bin/sass
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/bin/sass-convert
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/bin/scss

However, this list seems incomplete as I know there are files in

.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.2/lib/

Why does contents not show the files from lib?

Is it possible for a gem installer to install files outside of the gems folder?

Is there a command that can show everything installed by a gem?

Ruby Solutions


Solution 1 - Ruby

gem has an unpack command: http://guides.rubygems.org/command-reference/#gem-unpack

gem unpack rake
ls rake-0.4.3/

Solution 2 - Ruby

There are two really good ways to do this. There is another gem which allows you to open the gem and edit. This gem is call gem-open

gem install gem-open 

then

gem open sass

Another way is to generate your own rdocs.

gem rdoc sass

You can then look at your rdocs by

gem server

Also if you are using rvm, you can type rvm info and it will show GEM_HOME location. This will be where all your gems source code is.

cd $GEM_HOME
cd gems/sass-3.1.2/

Update:

This is the way I mostly do this now, when using bundler.

cd $(bundle show sass) 

This will be the version of sass in your Gemfile.

Solution 3 - Ruby

I usually open a gem by running this command from the console

EDITOR=<your editor> bundle open <name of gem>

Solution 4 - Ruby

The lib/ directory you mentioned is for version 3.1.2 of the gem; gem contents without --version will just list one version (it appears to pick the newest version, but I'm unable to verify this is always true). What output do you get for gem contents --version 3.1.2 sass?

Solution 5 - Ruby

You can also use just rename the .gem file to .tar and extract as a posix archive. The source code is inside it in the lib folder. See https://blog.srcclr.com/extracting-ruby-source-code-from-gem-packages/ for more details.

Solution 6 - Ruby

In addition to gem contents, another command you might find useful is gem environment. If you have multiple paths for your gem installations, they will all be listed under the "GEM PATHS" label.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlan StormView Question on Stackoverflow
Solution 1 - RubyErik PetersenView Answer on Stackoverflow
Solution 2 - RubyearlonrailsView Answer on Stackoverflow
Solution 3 - Rubyluis.madrigalView Answer on Stackoverflow
Solution 4 - RubyechristophersonView Answer on Stackoverflow
Solution 5 - RubycodelionView Answer on Stackoverflow
Solution 6 - RubyemeryView Answer on Stackoverflow