Find the parameters of each method in a class

C

Chris Gunnels

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?
 
J

Jason Roelofs

[Note: parts of this message were removed to make it a legal post.]

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
 
B

Benoit Daloze

In Ruby 1.9.2:

irb(main):001:0> class C
irb(main):002:1> def meth(a,bb, ccc =3D nil, &b)
irb(main):003:2> end
irb(main):004:1> end
=3D> nil
irb(main):005:0> C.instance_method:)meth).parameters
=3D> [[:req, :a], [:req, :bb], [:eek:pt, :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 Roelofs said:
You can find the arity of said methods, but afaik not the names of the
parameters:

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

Jason

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

class CoolClass
=A0def meth_one(p1,p2)
=A0...
=A0end

=A0def meth_two(p1,p2,p3)
=A0...
=A0end
end

I can run:
cc =3D 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?
 
C

Chris Gunnels

Benoit said:
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], [:eek:pt, :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 Roelofs <[email protected]>:

Dang thats awesome and exactly what I need! Why can't it be in 1.8? :(
 
R

Ryan Davis

Benoit said:
In Ruby 1.9.2:
=20
irb(main):001:0> class C
irb(main):002:1> def meth(a,bb, ccc =3D nil, &b)
irb(main):003:2> end
irb(main):004:1> end
=3D> nil
irb(main):005:0> C.instance_method:)meth).parameters
=3D> [[:req, :a], [:req, :bb], [:eek:pt, :ccc], [:block, :b]]
=20
But you will not get the names for C methods like String#tr, just the
type of the parameters.
=20
2010/1/26 Jason Roelofs <[email protected]>:
=20
Dang thats awesome and exactly what I need! Why can't it be in 1.8? :(

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

Roger Pack

irb(main):005:0> C.instance_method:)meth).parameters
=> [[:req, :a], [:req, :bb], [:eek:pt, :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? :(

rdp-arguments gem:
=> [[:a], [:bb], [:ccc, "nil"]]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,727
Latest member
FelicaTole

Latest Threads

Top