Skip to content

Instantly share code, notes, and snippets.

@drusepth
Created August 27, 2021 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drusepth/c865863a4b2313e1bb5c47518dfe9903 to your computer and use it in GitHub Desktop.
Save drusepth/c865863a4b2313e1bb5c47518dfe9903 to your computer and use it in GitHub Desktop.
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