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' 告訴我,默認值是 '誘人'。
任何人都知道解決方案嗎?
可能重複返回正確的默認值[獲取/設置參數的默認值動態](http://stackoverflow.com/questions/3873147/getting-setting-an-arguments-default-value-dynamically) –
它實際上是一個重複,但熱情地檢查在線索提供的答案:我發現我的恐懼,它不再活躍!我沒有足夠的觀點來提醒這個問題。 :( – user1266325
下面是該頁面的快照:http://web.archive.org/web/20100928212540/http://eigenclass.org/hiki/method+arguments+via+introspection - 注意,這是一個相當多毛的解決方案!;) –