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是怎么是实现的了



  • 相关阅读:
    不用+做加法
    实用类型转换
    Failed to retrieve application JMX service URL
    0.辗转相除法
    1. 数组与字符串
    Java数据结构之257二叉树的所有路径
    Java数据结构与算法之DFS
    Java数据结构与算法之图
    Java数据结构与算法之快速排序、归并排序
    Java数据结构与算法之冒泡排序、选择排序
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206855.html
Copyright © 2011-2022 走看看