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

后端 未结 2 523
终归单人心
终归单人心 2021-01-23 05:29

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

2条回答
  •  生来不讨喜
    2021-01-23 05:50

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

提交回复
热议问题