Viewing a Gem's Source Code

How can I see the source code of a gem installed on my machine?

Try gem unpack, it will copy source of a gem to current directory. For example,

gem unpack rails

Documentation: gem unpack.

Rails: Extract and Edit Source Code of Specific Gem

If you find an error in gem, you'd better make pull request on GitHub. But let's suppose you need your private fork of gem. Best workflow for that:

  1. Download the gem source code from GitHub: git clone https://github.com/author/awesome_gem.git.
  2. In your project's Gemfile add gem awesome_gem, path: "/local/path/to/awesome_gem"
  3. Run bundle install

Now you can make changes to the gem locally, and have your project use local copy of it. When you are done making initial changes, push your Gem to your github repository, and change Gemfile line to something like:

gem awesome_gem, github: 'QQQ/awesome_gem' ('QQQ' being your Github's account name)

How to Look at a Gem's Code

Gems on RubyGems usually have a link to the source code (most often on GitHub), in which case you can easily browse the code (I use this A LOT).

The "homepage" link also tends to link to the repository.

If all else fails, go to GitHub and search for the name of the gem (you may need to match up the authors to ensure it's the right repository for the gem).

Edit:

Just noticed that you asked about gems on Rubyforge. In which case my first step would be to check RubyGems. But otherwise you will need to download the gem and peek inside it (it's just a compressed archive so you can open it up in something like 7-zip).

Best way to search through the source code of all the gems for a project

I usually do the following:

cd `bundle show rails` # Go to the directory having rails gem content
cd .. # Go one level up to the folder having all gems
grep -ir escape_javascript * # search for the required text in all files
> actionview-4.1.6/lib/action_view/helpers/javascript_helper.rb: def escape_javascript(javascript)
> actionview-4.1.6/lib/action_view/helpers/javascript_helper.rb: alias_method :j, :escape_javascript

EDIT: The answer below by jrochkind is the correct answer; my answer is incorrect as it searches through all the gems installed in the system.

Ruby tool to browse through installed gems code

You could use pry with source-browsing.

It's a console similar to IRB, and you can view the definition of any class/method that is written in plain Ruby.

Here's an example with rgeo gem :

> pry                                                                                                               
[1] pry(main)> require 'rgeo'
=> true
[2] pry(main)> show-source RGeo::Cartesian.factory

From: ~/.rvm/gems/ruby-2.3.1/gems/rgeo-0.6.0/lib/rgeo/cartesian/interface.rb @ line 27:
Owner: #<Class:RGeo::Cartesian>
Visibility: public
Number of lines: 7

def preferred_factory(opts_ = {})
if ::RGeo::Geos.supported?
::RGeo::Geos.factory(opts_)
else
simple_factory(opts_)
end
end

How to open a gem source code with bundler in vim editor

There's a gem called qwandry. With this you can run

qw qwandry

to open up the qwandry source code in your editor (also configurable with the -e parameter). It also knows how to load up python, perl and node packages.

How do I make external libraries (gems) show up inside Rubymine's project view?

Go to File -> Settings -> Languages & Frameworks -> Ruby SDK and Gems. Then select the correct version of Ruby your code is using (in this case, 2.3.1). This should make the "External Libraries" section on project window to load from the correct version of gem home. Restart Rubymine if necessary.

Optimal Way to Browse Gem Source Code

Aptana Studio 3 has a command line launcher: studio3.

Add the following to your ~/.bash_profile:

export PATH="/Applications/Aptana Studio 3:$PATH"
export GEM_EDITOR="studio3"

Reload your existing shell environment: . ~/.bash_profile and then you can use gem-open with your preferred editor: gem open rails

How do I browse the source code for a gem in Rails3 with RVM (linux & gedit)

CubaLibre's response got me half-way there. The other piece of the puzzle I was able to get from Ask Ubuntu.

Here are the steps to browse gem code in gedit using Ubuntu.

a. From the command line run:

gnome-open $(bundle show [gem-name])

b. This will open up the gem directory in nautilus. From there just double click on file you'd like to see and it will open up in gedit.



Related Topics



Leave a reply



Submit