这是我模块的摘录:
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
Helpers.reflect()
打印调试信息。而不是然后在每个函数Helpers.reflect binding, self.to_s
中进行显式编写,是否有任何方法指示ruby在模块中的所有函数(Helpers.reflect binding, self.to_s
本身除外)之前执行Helpers.reflect
并避免显式编写? / li>
Helpers.reflect()
方法得到改进,为其提供更少(或零)的参数?