Methods 如何在JRuby中检索给定方法定义的默认参数值
如何检索给定方法的完整参数定义,包括默认值 我真的很惊讶这不在标准库中;但也许有办法 我已经签出了一个名为“get_args”的merb库,但这似乎是一个旧的失败测试。 下面的示例中使用的parameters方法是一种很有前途的方法,但(如下面的输出所示)它不提供有关默认值的信息 例如:Methods 如何在JRuby中检索给定方法定义的默认参数值,methods,parameters,arguments,jruby,default,Methods,Parameters,Arguments,Jruby,Default,如何检索给定方法的完整参数定义,包括默认值 我真的很惊讶这不在标准库中;但也许有办法 我已经签出了一个名为“get_args”的merb库,但这似乎是一个旧的失败测试。 下面的示例中使用的parameters方法是一种很有前途的方法,但(如下面的输出所示)它不提供有关默认值的信息 例如: require 'pp' class Siren def woo(song_style = "tantalising", *other_sailors, target_sailor) puts
require 'pp'
class Siren
def woo(song_style = "tantalising", *other_sailors, target_sailor)
puts "wooing #{target_sailor} with the #{song_style} song, whilst winking at: #{other_sailors.size} others."
end
end
sally = Siren.new
sally.woo("John")
puts
pp Siren.instance_method(:woo).parameters
输出:
wooing John with the tantalising song, whilst winking at: 0 others.
[[:opt, :song_style], [:rest, :other_sailors], [:req, :target_sailor]]
但我希望“宋_风格”告诉我默认的是“诱人”
有人知道解决方案吗?这不是100%可能的,因此Ruby的API不支持。。。 e、 g.如果该方法看起来像什么(取决于实例或想象它在另一个对象上调用某个“外部”方法):
在这种情况下,通过内省根本不可能返回正确的默认值它的可能副本实际上是一个副本,但在热情地检查了该线程上提供的答案后:我发现它不再是活动的,我感到震惊!我还没有足够的要点来提醒这个问题(这是该页面的快照:–小心,这是一个相当棘手的解决方案!)
def woo(song_style = muu)
# ...
end