Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
How to find the exact line of code where a yield is returned?
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
end

In the above code, yield returns at next if a == 1, how to get the line number of source code where next was invoked in the given block from the test_method?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

For debugging purposes, you can use TracePoint to monitor the executed lines:

# 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                            # 23

Output:

#<TracePoint:line@test.rb:9 in `test_method'>
#<TracePoint:line@test.rb:18 in `another_method'>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!