Skip to content

Instantly share code, notes, and snippets.

@markburns
Created June 7, 2017 11:04
Show Gist options
  • Select an option

  • Save markburns/632a50228d3bf9ba43d0ff62ce2adc33 to your computer and use it in GitHub Desktop.

Select an option

Save markburns/632a50228d3bf9ba43d0ff62ce2adc33 to your computer and use it in GitHub Desktop.
require "binding_of_caller"
Thread.current[:current_thread_local_info] = "value set inside (main)"
class A
def something
t = Thread.new do
Thread.current[:current_thread_local_info] = "value set inside A#something"
B.new.another_thing
end
t.join
end
end
class B
def another_thing
Thread.current[:current_thread_local_info] = "value set inside B#another_thing"
1
end
end
trace = TracePoint.new(:call) do |tp|
puts "-" * 80
t = Thread.current
puts "current_thread_local_info: #{t[:current_thread_local_info]}"
if tp.defined_class == A || tp.defined_class == B
puts "self: #{tp.self}"
puts " line: #{tp.lineno}"
puts " file: #{tp.path}"
puts " class: #{tp.defined_class}"
puts " method: #{tp.method_id}"
show_caller = lambda do |s, label=nil|
puts "#{label || s}: #{binding.of_caller(3).eval(s)}"
end
show_caller.('self', "Caller")
show_caller.(' __FILE__')
show_caller.(' __LINE__')
show_caller.(' __method__')
show_caller.('self.is_a?(Module) ? self : self.class', ' class')
end
end
trace.enable
A.new.something
puts Thread.current[:current_thread_local_info]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment