您的位置:首页 > 其它

如何查找某一函数的定义

2008-06-11 14:32 155 查看
今天在学《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的源代码和注释了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: