def test_method result = [] result << yield # How to get the line number of where next was invoked in this block puts caller[0] # Gives the line number of another_method result end def another_method a = 1 b = 2 test_method do next if a == 1 next if b == 200 end endEn el código anterior, el rendimiento devuelve el siguiente si a == 1 , ¿cómo obtener el número de línea del código fuente donde se invocó el siguiente en el bloque dado desde test_method?
Con fines de depuración, puede usar TracePoint para monitorear las líneas ejecutadas:
# test.rb # 1 # 2 def test_method # 3 result = [] # 4 trace = TracePoint.new(:line) do |tp| # 5 p tp # 6 end # 7 trace.enable do # 8 result << yield # 9 end # 10 result # 11 end # 12 # 13 def another_method # 14 a = 1 # 15 b = 2 # 16 test_method do # 17 next if a == 1 # 18 next if b == 200 # 19 end # 20 end # 21 # 22 another_method # 23Producción:
#<TracePoint:line@test.rb:9 in `test_method'> #<TracePoint:line@test.rb:18 in `another_method'>