Created
August 27, 2021 07:52
-
-
Save drusepth/05815bcd8c0481211c67f169d2e4449c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'pry' | |
# APPROACH 1 | |
module PerfLogger | |
def perflog(func_name) | |
new_name_for_old_function = "#{func_name}_old".to_sym | |
alias_method(new_name_for_old_function, func_name) | |
define_method(func_name) do |*args| | |
puts "about to call #{func_name}(#{args.join(', ')})" | |
send(new_name_for_old_function, *args) | |
end | |
end | |
end | |
class ExampleClass | |
extend PerfLogger | |
def instance_method(foo, bar) | |
puts "In the instance method with foo=#{foo} & bar=#{bar}" | |
end | |
perflog(:instance_method) | |
def self.class_method(foo, bar) | |
puts "In the class method with foo=#{foo} & bar=#{bar}" | |
end | |
# perflog(:class_method) | |
end | |
def bare_method(foo, bar) | |
puts "In the bare method with foo=#{foo} & bar=#{bar}" | |
end | |
# perflog(:bare_method) | |
# ExampleClass.new.instance_method("Uno", "Dos") | |
# ExampleClass.class_method("Tres", "Quatro") | |
# bare_method("Cinco", "Seis") | |
# APPROACH 2 | |
def perflog(method_name) | |
method = method(name) | |
puts "in flog hook" | |
end | |
perflog def method_to_perflog(foo, bar) | |
puts "in method_to_perflog with foo=#{foo} & bar=#{bar}" | |
end | |
puts "methods hooked" | |
method_to_perflog("biz", "baz") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment