简体   繁体   English

在Ruby中有效地浏览源代码

[英]Navigating through source code efficiently in Ruby

I was trying to find the method being called when Item.where(dst: "video") is called (Item being a Mongoid model). 当我调用Item.where(dst: "video")时,我试图找到被调用的方法(Item是Mongoid模型)。 Looking up in the source code, I see that criteria.rb is the place to go to. 查看源代码,我看到criteria.rb是要去的地方。 However, def where calls super. 但是, def where调用超级。 Then Origin::Selectable (included inside Origin::Queryable ) defines it: 然后Origin::Selectable (包含在Origin::Queryable )定义它:

def where(criterion = nil)
   criterion.is_a?(String) ? js_query(criterion) : expr_query(criterion)
end

Now, I would have to see where js_query and expr_query are, see what they do and so on. 现在,我必须看看js_queryexpr_query在哪里,看看他们做了什么,等等。

It gets tough going through all this source code and modules, finding all the methods and then trying to figure out how it works. 通过所有这些源代码和模块,找到所有方法,然后试图弄清楚它是如何工作的。

Is there a better way to do this process to find out how things work? 有没有更好的方法来完成这个过程以找出工作原理?

You probably need to improve your editor experience. 您可能需要改善编辑器体验。 There are three remarkable abilities (besides many others like Eclipse/Aptana , NetBeans , etc): 有三个非凡的能力(除了许多其他像Eclipse / AptanaNetBeans等):

Depending on your choice you yield an ability to quickly navigate through your code with either Ctrl + Click or with your preferred keyboard shortcut. 根据您的选择,您可以使用Ctrl + Click或首选键盘快捷键快速浏览代码。

Here on SO this question was asked an amount of times as well: https://stackoverflow.com/search?q=best+ruby+editor 在这个问题上,这个问题被问了很多次: https//stackoverflow.com/search?q = best +ruby +editor

Hope it helps. 希望能帮助到你。

If you know the class of the receiver (say A ) and the method name (say foo ), then you can do: 如果您知道接收器的类(比如A )和方法名(比如foo ),那么您可以这样做:

A.instance_method(:foo).source_location

That will give the file name and the line number in most cases. 在大多数情况下,这将给出文件名和行号。 If it returns nil , then it is likely a C-defined method, which does not rely on other Ruby methods. 如果它返回nil ,那么它很可能是一个C定义的方法,它不依赖于其他Ruby方法。

Another way is to use the pry gem or the method_source gem. 另一种方法是使用pry宝石或method_source宝石。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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