-
-
Save XrXr/f6cc2f1a4b6d3341e3d3fc749bcd5255 to your computer and use it in GitHub Desktop.
Demo script to show that the effect of tracing an instance method is global across all instances of that class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
def hello(arg = nil) | |
puts "dale sent me" | |
end | |
end | |
one = Foo.new | |
two = Foo.new | |
one.hello | |
two.hello | |
# so this is a weird one, the call event happens right after | |
# entering a method instead of before a call happens, as one | |
# might intuitively expect | |
trace = TracePoint.new(:call) { |tp| puts "intercepted! arg=#{tp.binding.local_variable_get(:arg)}" } | |
trace.enable(target: Foo.instance_method(:hello)) do | |
one.hello(:first) | |
two.hello(:second) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[tmp]# ruby -v affect-all-instances.rb | |
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux] | |
dale sent me | |
dale sent me | |
intercepted! arg=first | |
dale sent me | |
intercepted! arg=second | |
dale sent me |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment