如何查找某一函数的定义

今天在学《Agile Web Development with Rails, 2nd ed》的functional testing of controllers一节的时候。遇到了模拟浏览器get方法的一个函数,get。它的第三个参数可以往session里面放东西。我看的书是beta版,没有对get的参数列表的解释。开始我以为在rails文档库中可以找到定义。谁知道竟然没有。所以只能自已定位了。

原书上的函数调用代码为:
get :index, {}, { :user_id => users(:dave).id }

为了查找调用的这个get到底是哪个类的。我使用了set_trace_func这个函数。这个函数在ruby one-click installer安装后附带的《Programming Ruby》里详细介绍。它是Kernel模块的一个方法。在ruby的core文档库里可以找到说明。使用它,源码为:
    set_trace_func proc { |event, file, line, id, binding, classname|
      if (id.to_s == "get")
        printf "%8s %s:%-2d %10s %8s"n", event, file, line, id, classname
      end
    }
    get :index, {}, { :user_id => users(:dave).id }
    set_trace_func nil

这样,我就可以在控制台运行测试程序的时候查看输出了。输出为:
Loaded suite test/functional/login_controller_test
Started
    call c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/test_process.rb:354        get Test::Unit::TestCase
    line c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/test_process.rb:355        get Test::Unit::TestCase
    line c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/test_process.rb:355        get Test::Unit::TestCase
    line c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/test_process.rb:356        get Test::Unit::TestCase
  return c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/test_process.rb:355        get Test::Unit::TestCase
..
Finished in 6.499 seconds.

2 tests, 6 assertions, 0 failures, 0 errors

这样,我就可以在test_process.rb里找到get的源代码和注释了。

转载于:https://www.cnblogs.com/ydong/archive/2008/06/11/1217372.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值