简体   繁体   中英

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

I installed Devise in my Rails app and I want to read through the source code and learn how it works. I looked through the entire folder structure of my Rails app, but couldn't find any code (except for the method calls).

I know I can see the source from the Github repository but I would like to see it in my editor and on my local machine.

I'm guessing this code must be in some main Ruby directory, but I'm having trouble locating it. Any help is appreciated. Thanks.

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

gem unpack rails

Documentation: gem unpack .

Besides Sergio's suggestion, there is another option.

Within your Rails path

$ bundle open devise

This will open the installed gem in editor with the version specified in Gemfile, very handy.

Simply run bundle show <gem-name> , it will list the absolute path of gem source code and in next step simply open source code using text editor like this subl <gem-code-absolute-path>

For Example

Let's assume you want to read kaminari gem code

bundle show kaminari

/home/abdullah/.rvm/gems/ruby-2.3.0@your_gem_name/gems/kaminari-0.16.3

next step ( subl is command to open with Sublime Text Editor)

subl /home/abdullah/.rvm/gems/ruby-2.3.0@your_gem_name/gems/kaminari-0.16.3

Run gem environment - this will display you all the information about your gems, including their location.

Additionally I would advise you to install some IDE with go to source feature - RubyMine is just brilliant (and has 30-day-long free trial), if you want to go for absolutely free go with NetBeans together with Ruby plugin . This feature allows you to navigate quickly to source of clicked method, regardless whether it is defined inside your code or inside the gem.

在本地计算机中克隆github repo并使用您喜欢的编辑器进行探索:

git clone https://github.com/plataformatec/devise.git

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM