[英]Ruby TracePoint: How to trap definition of a specific class?
我試圖找出如何使用Ruby的TracePoint API來捕獲特定類(例如“ Resolv”)的定義和后續重新定義。 我能夠使用以下方法捕獲所有類定義:
TracePoint.trace(:class) do |tp|
require 'pry'; binding.pry # for example
end
但是,我無法使用:class
或:end
參數對其進行過濾,因此只能捕獲Resolv
類。 該TracePoint
對象有一個defined_class
屬性,但包含誰在類的定義(這是時間自nil
,又名(main)
),而不是它的定義是要被處理的級別。 我也找不到找到正在處理哪個文件和行的方法。 有一個binding
變量,但它不包含變量。
我怎樣才能做到這一點?
我知道的唯一方法是跟蹤所有類定義,並使用TracePoint#self
對其進行過濾:
TracePoint.new(:end) do |tp|
if tp.self == Resolv
# yay, we are in
# tp.disable # use this to unset a trace point
end
end.enable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.