Last active
March 26, 2021 18:19
-
-
Save mwlang/6182f97ce9bcc5cdd1d4e9551b03f1ba to your computer and use it in GitHub Desktop.
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
FOO | |
==================== | |
Hello | |
World | |
******************************************************************************** | |
Traced! | |
Traced! | |
Hello | |
World | |
BAR | |
==================== | |
hello | |
world | |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
Traced! | |
hello | |
Traced! | |
world | |
FOOBAR | |
==================== | |
HELLO | |
WORLD | |
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& | |
Traced! | |
HELLO | |
Traced! | |
WORLD | |
FOOBAZ | |
==================== | |
HeLlo | |
WoRlD | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
Traced! | |
HeLlo | |
Traced! | |
WoRlD |
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 Foobar | |
def hello; puts "HELLO" end | |
def world; puts "WORLD" end | |
def speak; hello; world end | |
end | |
class Foo | |
def hello; puts "Hello" end | |
def world; puts "World" end | |
def speak; hello; world end | |
end | |
class Bar | |
def hello; puts "hello" end | |
def world; puts "world" end | |
def speak; hello; world end | |
end | |
class Foobaz | |
def hello; puts "HeLlo" end | |
def world; puts "WoRlD" end | |
def speak; hello; world end | |
end | |
module Tracer | |
module_function | |
def with_tracing; puts "Traced!"; yield end | |
end | |
module StaticInstrument | |
include Tracer | |
def hello; with_tracing { super } end | |
def speak; with_tracing { super } end | |
end | |
module MetaInstrument | |
include Tracer | |
%w{hello world}.each do |method_name| | |
define_method(method_name) do |*args, &block| | |
with_tracing { super(*args, &block) } | |
end | |
end | |
end | |
puts "FOO", "=" * 20 | |
Foo.new.speak | |
puts "*" * 80 | |
Foo.prepend StaticInstrument | |
Foo.new.speak | |
puts "", "BAR", "=" * 20 | |
Bar.new.speak | |
puts "%" * 80 | |
Bar.prepend MetaInstrument | |
Bar.new.speak | |
puts "", "FOOBAR", "=" * 20 | |
Foobar.new.speak | |
puts "&" * 80 | |
Foobar.class_eval do | |
include Tracer | |
alias_method :hello_without_tracing, :hello | |
def hello | |
with_tracing { hello_without_tracing } | |
end | |
alias_method :world_without_tracing, :world | |
def world | |
with_tracing { world_without_tracing } | |
end | |
end | |
Foobar.new.speak | |
puts "", "FOOBAZ", "=" * 20 | |
Foobaz.new.speak | |
def dynamic_tracer_module method_names | |
Module.new do | |
include Tracer | |
method_names.each do |method_name| | |
define_method(method_name) do |*args, &block| | |
with_tracing { super(*args, &block) } | |
end | |
end | |
end | |
end | |
puts "!" * 40 | |
Foobaz.prepend dynamic_tracer_module %w{hello world} | |
Foobaz.new.speak |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment