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

后端 未结 2 522
终归单人心
终归单人心 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]]
    
    0 讨论(0)
  • 2021-01-23 05:59

    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.

    0 讨论(0)
提交回复
热议问题