rubyにはメソッドのソースを取得する method_source というgemがあるのですが、これのクラスバージョンもあったら便利かも?と思って書いてみました。 module ClassSource @lines_for_file = {} @locations = Hash.new { |h, k| h[k] = [] } @pos_stack = [] TracePoint.trace(:class, :end) do |tp| next if tp.self == ClassSource @lines_for_file[tp.path] ||= File.readlines(tp.path) case tp.event when :class @pos_stack << tp.lineno - 1 when :end from, to = @pos_stack.pop, tp.li