Here is an excerpt from my module:
module Helpers
def self.reflect(callers_binding, callers_name)
callers_method = caller_locations.first.label
callers_instance = Module.const_get callers_name
callers_parameters = callers_instance
.method(callers_method)
.parameters.map do |_, arg_name|
[arg_name, callers_binding.local_variable_get(arg_name)]
end
callers_parameters = Hash[callers_parameters]
puts "#{callers_instance}.#{callers_method}: #{callers_parameters}"
end
end
module MyModule
def self.foo(arg1, arg2)
Helpers.reflect binding, self.to_s
puts arg1, arg2
end
def self.bar(arg1, arg2, arg3)
Helpers.reflect binding, self.to_s
puts arg1, arg2, arg3
end
end
- As seen, I would like to print a debug information using
Helpers.reflect()
. rather then writing explicitly in each functionHelpers.reflect binding, self.to_s
, is there any way to instruct ruby to executeHelpers.reflect binding, self.to_s
before all functions in the module (except forHelpers.reflect
itself) and avoiding writing it explicitly? - How can
Helpers.reflect()
method be improved, providing less (or zero) argument to it?