如何获取 Ruby 中使用的方法列表?
How to get a list of used methods in Ruby?
嘿,我希望我的方法 logify 将每个方法与其参数和我的 class A 的 return 值放在一起。例如,我写了一个简单的 class A 和两个方法添加和 sub,输出应该是这样的:
Output:
Method add(1, 2) called
return value 3
Method sub(1, 2) called
return value -1
我知道我可以通过 self.instance_methods(false)
获得每个方法,但是有人可以进一步帮助我吗?
require_relative "log"
class A
extend Log
def add(a, b)
a + b
end
def sub(a, b)
a - b
end
logify
end
a = A.new
a.add(2,1)
a.sub(2,1)
module Log
def logify
puts self.instance_methods(false)
end
end
您可以使用 Module#prepend
和 Module#prepended
来帮助解决这个问题,例如:
module Log
def self.prepended(base)
base.instance_methods(false).each do |m|
define_method(m) do |*args, &block|
puts "Method #{m}(#{args.join(',')}) called"
val = super(*args, &block)
puts "return value #{val}"
val
end
end
end
end
class A
def add(a, b)
a + b
end
def sub(a, b)
a - b
end
end
A.prepend(Log)
它的作用是在前置模块中定义一个与原始方法同名的方法,然后构建输出并删除中间的原始方法 (super
) 以获得 return值。
例子
a = A.new
a.add(2,1)
# Method add(2,1) called
# return value 3
#=> 3
a.sub(2,1)
# Method sub(2,1) called
# return value 1
#=> 1
注意:这只会显示提供的参数,不会在方法签名中输出默认参数
ruby 核心库包括 class TracePoint
,它可用于跟踪几乎所有内容 - 从定义或调用的方法,或引发的异常,. ..
这是一个示例用法,它将执行您想要的跟踪:
class A
def add(a, b)
a + b
end
def sub(a, b)
a - b
end
end
TracePoint.trace(:call, :return) do |tp|
next unless tp.defined_class == A
case tp.event
when :call
params = tp.parameters.map { |arg| eval(arg[1].to_s, tp.binding) }
puts "Method #{tp.method_id}(#{params.join(', ')}) called"
when :return
puts "return value #{tp.return_value}"
end
end
# The trace has been enabled! Any time one of those events occurs, the block is evaluated.
a = A.new
a.add(2,1)
a.sub(2,1)
输出:
Method add(2, 1) called
return value 3
Method sub(2, 1) called
return value 1
如您所见,获取 params
数据有点麻烦。 TracePoint
可以访问方法 signature,但您需要使用跟踪的 binding 来查看它实际被调用的值与.
嘿,我希望我的方法 logify 将每个方法与其参数和我的 class A 的 return 值放在一起。例如,我写了一个简单的 class A 和两个方法添加和 sub,输出应该是这样的:
Output:
Method add(1, 2) called
return value 3
Method sub(1, 2) called
return value -1
我知道我可以通过 self.instance_methods(false)
获得每个方法,但是有人可以进一步帮助我吗?
require_relative "log"
class A
extend Log
def add(a, b)
a + b
end
def sub(a, b)
a - b
end
logify
end
a = A.new
a.add(2,1)
a.sub(2,1)
module Log
def logify
puts self.instance_methods(false)
end
end
您可以使用 Module#prepend
和 Module#prepended
来帮助解决这个问题,例如:
module Log
def self.prepended(base)
base.instance_methods(false).each do |m|
define_method(m) do |*args, &block|
puts "Method #{m}(#{args.join(',')}) called"
val = super(*args, &block)
puts "return value #{val}"
val
end
end
end
end
class A
def add(a, b)
a + b
end
def sub(a, b)
a - b
end
end
A.prepend(Log)
它的作用是在前置模块中定义一个与原始方法同名的方法,然后构建输出并删除中间的原始方法 (super
) 以获得 return值。
例子
a = A.new
a.add(2,1)
# Method add(2,1) called
# return value 3
#=> 3
a.sub(2,1)
# Method sub(2,1) called
# return value 1
#=> 1
注意:这只会显示提供的参数,不会在方法签名中输出默认参数
ruby 核心库包括 class TracePoint
,它可用于跟踪几乎所有内容 - 从定义或调用的方法,或引发的异常,. ..
这是一个示例用法,它将执行您想要的跟踪:
class A
def add(a, b)
a + b
end
def sub(a, b)
a - b
end
end
TracePoint.trace(:call, :return) do |tp|
next unless tp.defined_class == A
case tp.event
when :call
params = tp.parameters.map { |arg| eval(arg[1].to_s, tp.binding) }
puts "Method #{tp.method_id}(#{params.join(', ')}) called"
when :return
puts "return value #{tp.return_value}"
end
end
# The trace has been enabled! Any time one of those events occurs, the block is evaluated.
a = A.new
a.add(2,1)
a.sub(2,1)
输出:
Method add(2, 1) called
return value 3
Method sub(2, 1) called
return value 1
如您所见,获取 params
数据有点麻烦。 TracePoint
可以访问方法 signature,但您需要使用跟踪的 binding 来查看它实际被调用的值与.