2

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

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
millisami
  • 9,931
  • 15
  • 70
  • 112
  • This is a duplicate of [Is there a way to return a method parameter names in ruby](http://StackOverflow.Com/q/2452077/#2452322), [Reflection on method parameters in Ruby](http://StackOverflow.Com/q/3456827/#3460072). – Jörg W Mittag Aug 21 '11 at 10:17

2 Answers2

7

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.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
3

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]]
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 1
    Didn't know you could do that. I'd really like it if Ruby listed the names of the parameters when you have an ArgumentError because you have the wrong number of arguments. – Andrew Grimm Jun 22 '11 at 00:46