zoukankan      html  css  js  c++  java
  • 查找ruby方法(以rails为例)

    我们知道ruby使用动态方法,还有一些mixin等功能,所以查找ruby的方法变得比一般的语言更困难,高富帅可以用textmate,屌丝没得。主要的想法是使用grep。


    grep [options] [patten] [files]


    在使用linux命令查找ruby方法时,更多的时候需要使用-rn的参数就足够,r表示recursive, n表式行号


    可以先clone一份最新版本的rails代码作为实验:git@github.com:rails/rails.git


    首先进入到rails/activereocrd目录下,我们知道ActiveRecord类提供了如find_by这个方法,我们就查找find_by这个方法。得到结果:

    kiwi@kiwi-K43SV:~/rails/activerecord$ grep -rn "def find_by" .

    ./lib/active_record/associations/collection_association.rb:569: def find_by_scan(*args)

    ./lib/active_record/querying.rb:37: def find_by_sql(sql, binds = [])

    ./lib/active_record/relation/finder_methods.rb:47: def find_by(*args)

    ./lib/active_record/relation/finder_methods.rb:53: def find_by!(*args)


    这样就能够找到find_by方法了。这种是最容易的情况。


    另一种情况是我们想查找被delegate的方法:假设我们要查找的是ActiveRecord中的last方法

    kiwi@kiwi-K43SV:~/rails/activerecord$ grep -rn "delegate.\+:last" .

    ./lib/active_record/querying.rb:4: delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :to => :all


    即找出来被delegate了:all

    这样再使用define的搜索方式就能找到all是怎么是实现的了



  • 相关阅读:
    找了半天的问题
    一个图片加载与绘制类(使用GDI输出图片)
    GetBitmapBits和GetDIBits的区别(Windows GDI)
    VBScripts and UAC elevation
    win32程序中简单应用mfc
    gc buffer busy等待事件
    global cache cr request等待事件
    利用Cluster Verify Utility工具体验RAC最佳实践
    食物日记:上海铁板烧
    gc cr request等待事件
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206855.html
Copyright © 2011-2022 走看看