DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Finds The File And Line Of Executed Code

04.30.2007
| 2569 views |
  • submit to reddit
        
module Kernel
  def which(&block)
    result = 'No ruby call has happen in the given block!'
    found = false
    set_trace_func lambda { |event, file, line, id, binding, classname|
      return unless event == 'call'
      result = "#{file}:#{line}: " unless found
      found = true
    }
    yield
    set_trace_func(nil)
    result
  end
end

# test
require 'yaml'
puts which { [].to_yaml }