2014-03-13 55 views
0

如何检索给定方法的完整参数定义,包括默认值?如何在JRuby中检索给定方法定义的默认参数值

我真的很惊讶这不是在标准库中;但也许有办法?

我已经检出了名为'get_args'的merb库,但是这似乎是旧的并且未通过测试。 一个有希望的方法是在下面的例子中使用的参数方法,但是(如下面的输出所示),这不提供有关默认值的信息。

例子:

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]] 

不过我倒是喜欢 'song_style' 告诉我,默认值是 '诱人'。

任何人都知道解决方案吗?

+0

可能重复返回正确的默认值[获取/设置参数的默认值动态](http://stackoverflow.com/questions/3873147/getting-setting-an-arguments-default-value-dynamically) –

+0

它实际上是一个重复,但热情地检查在线索提供的答案:我发现我的恐惧,它不再活跃!我没有足够的观点来提醒这个问题。 :( – user1266325

+0

下面是该页面的快照:http://web.archive.org/web/20100928212540/http://eigenclass.org/hiki/method+arguments+via+introspection - 注意,这是一个相当多毛的解决方案!;) –

回答

0

这不是100%可能的,因此在Ruby的API中不支持...... 如果有什么方法看(根据实例或想象它调用一些“外部”的另一个对象的方法),如:在这种情况下

def woo(song_style = muu) 
    # ... 
end 

这是根本不可能通过内省

相关问题