如何查看我机器上安装的gem的源代码?

时间:2022-01-15 07:11:32

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

我在我的Rails应用程序中安装了Devise,我想阅读源代码并了解它是如何工作的。我查看了我的Rails应用程序的整个文件夹结构,但找不到任何代码(方法调用除外)。

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.

我知道我可以从Github存储库中看到源代码,但我希望在我的编辑器和本地机器上看到它。

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

我猜这个代码必须在一些主要的Ruby目录中,但我找不到它。任何帮助表示赞赏。谢谢。

5 个解决方案

#1


7  

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

尝试gem解包,它会将gem的源代码复制到当前目录。例如,

gem unpack rails

Documentation: gem unpack.

文档:gem unpack。

#2


7  

Besides Sergio's suggestion, there is another option.

除了塞尔吉奥的建议,还有另一种选择。

Within your Rails path

在你的Rails路径中

$ bundle open devise

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

这将使用Gemfile中指定的版本在编辑器中打开已安装的gem,非常方便。

#3


2  

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>

只需运行bundle show ,它将列出gem源代码的绝对路径,并在下一步中使用文本编辑器简单地打开源代码,例如这个subl

For Example

Let's assume you want to read kaminari gem code

我们假设您想要阅读kaminari gem代码

bundle show kaminari

捆绑显示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是使用Sublime Text Editor打开的命令)

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

#4


1  

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

运行gem环境 - 这将显示有关您的宝石的所有信息,包括它们的位置。

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.

另外我建议你安装一些带有go-source功能的IDE - RubyMine非常出色(并且有30天的免费试用版),如果你想完全免费使用NetBeans和Ruby插件。此功能允许您快速导航到单击方法的源,无论它是在代码内部还是在gem内部定义。

#5


0  

Clone the github repo in your local machine and explore it using your prefered editor:

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

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

#1


7  

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

尝试gem解包,它会将gem的源代码复制到当前目录。例如,

gem unpack rails

Documentation: gem unpack.

文档:gem unpack。

#2


7  

Besides Sergio's suggestion, there is another option.

除了塞尔吉奥的建议,还有另一种选择。

Within your Rails path

在你的Rails路径中

$ bundle open devise

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

这将使用Gemfile中指定的版本在编辑器中打开已安装的gem,非常方便。

#3


2  

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>

只需运行bundle show ,它将列出gem源代码的绝对路径,并在下一步中使用文本编辑器简单地打开源代码,例如这个subl

For Example

Let's assume you want to read kaminari gem code

我们假设您想要阅读kaminari gem代码

bundle show kaminari

捆绑显示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是使用Sublime Text Editor打开的命令)

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

#4


1  

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

运行gem环境 - 这将显示有关您的宝石的所有信息,包括它们的位置。

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.

另外我建议你安装一些带有go-source功能的IDE - RubyMine非常出色(并且有30天的免费试用版),如果你想完全免费使用NetBeans和Ruby插件。此功能允许您快速导航到单击方法的源,无论它是在代码内部还是在gem内部定义。

#5


0  

Clone the github repo in your local machine and explore it using your prefered editor:

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

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