Any ruby library to inspect what are the arguments that a certain methods take?

 ̄綄美尐妖づ 提交于 2019-12-20 03:04:26

问题


is there any library that can inspect and display what are the arguments that a method takes?


回答1:


I don't know of any third-party libraries or even RubyGems that can do this reliably. It just isn't possible to do this kind of inspection given the limited reflection facilities Ruby provides.

You will have to make do with what is available in the core library, which is basically Method#parameters:

def foo(a, b=nil, *c, d, &e); end
method(:foo).parameters
  # => [[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]]



回答2:


There's Method#arity that lists how many arguments a method can take.

However, you can't determine what types of objects a method expects. That just isn't in the nature of Ruby.



来源:https://stackoverflow.com/questions/6420695/any-ruby-library-to-inspect-what-are-the-arguments-that-a-certain-methods-take

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!