Find the parameters of each method in a class

Is there a way to find the parameters of each method in a class? For
instance I have a class like:

class CoolClass
def meth_one(p1,p2)

end

def meth_two(p1,p2,p3)

end
end

I can run:
cc = CoolClass.new
cc.public_methods(false)

and it will return an array of all the public methods
([‘meth_one’,‘meth_two’]).

Is there a way to find the parameters of each method from the example
above?

You can find the arity of said methods, but afaik not the names of the
parameters:

CoolClass.instance_method(:method_one).arity # => 2
CoolClass.instance_method(:method_two).arity # => 3

Jason

Thanks Jason. That gets me a little further. For those that don’t know,
arity returns an indication of the number of arguments accepted by a
method.

http://ruby-doc.org/core/classes/UnboundMethod.html#M001131

Benoit D. wrote:

In Ruby 1.9.2:

irb(main):001:0> class C
irb(main):002:1> def meth(a,bb, ccc = nil, &b)
irb(main):003:2> end
irb(main):004:1> end
=> nil
irb(main):005:0> C.instance_method(:meth).parameters
=> [[:req, :a], [:req, :bb], [:opt, :ccc], [:block, :b]]

But you will not get the names for C methods like String#tr, just the
type of the parameters.

2010/1/26 Jason R. [email protected]:

Dang thats awesome and exactly what I need! Why can’t it be in 1.8? :frowning:

On Jan 26, 2010, at 13:30 , Chris G. wrote:

But you will not get the names for C methods like String#tr, just the
type of the parameters.

2010/1/26 Jason R. [email protected]:

Dang thats awesome and exactly what I need! Why can’t it be in 1.8? :frowning:

It can. Look at merb. I think it is in merb-param-protection. Much more
hacky, but it works.

irb(main):005:0> C.instance_method(:meth).parameters
=> [[:req, :a], [:req, :bb], [:opt, :ccc], [:block, :b]]

But you will not get the names for C methods like String#tr, just the
type of the parameters.
Dang thats awesome and exactly what I need! Why can’t it be in 1.8? :frowning:

rdp-arguments gem:

require ‘arguments’
class C

def meth(a, bb, ccc = nil, &b)
end
end
=> nil

Arguments.names C, :meth
=> [[:a], [:bb], [:ccc, “nil”]]

You can get it in 1.9.1 with the methopara gem, too.
GL!
-r

In Ruby 1.9.2:

irb(main):001:0> class C
irb(main):002:1> def meth(a,bb, ccc = nil, &b)
irb(main):003:2> end
irb(main):004:1> end
=> nil
irb(main):005:0> C.instance_method(:meth).parameters
=> [[:req, :a], [:req, :bb], [:opt, :ccc], [:block, :b]]

But you will not get the names for C methods like String#tr, just the
type of the parameters.

2010/1/26 Jason R. [email protected]:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs