如何查找某一函数的定义
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的源代码和注释了。
原书上的函数调用代码为:
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的源代码和注释了。
相关文章推荐
- Linux 下如何快速查找到头文件和函数定义
- 如何查找Linux的函数定义的位置?
- 如何定义立即执行函数?【转】
- 浅谈如何定义和调用Python的函数
- [转帖]Linux下如何查找一个函数在哪个库中
- javascript是没有类的概念,那么对象是如何定义的,没错,就是函数!
- 使用在类中定义的有返回值的函数,如何得到执行结果反馈?
- 如何定义和实现一个类的成员函数为回调函数?
- ABAP--如何在ALV_Grid的函数中定义下拉列表
- 如何使用函数公式来查找图片
- linux下如何查找函数所在的库文件
- 如何定义和实现一个类的成员函数为回调函数
- MATLAB如何定义函数
- ABAP--如何在ALV_Grid的函数中定义下拉列表
- 在form中调用另一个form的方法,函数:fnd_function.execute 如何在form1中调用form2呢?可以使用函数fnd_function.execute。其函数定义如下: fn
- python如何查找函数文档
- 在MFC下如何定义全局变量和全局函数
- 如何运用PHP stristr()函数进行字符查找
- C++:如何声明和定义成员函数
- eval 如何定义函数