Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Find the parameters of each method in a class

Reply
Thread Tools

Find the parameters of each method in a class

 
 
Chris Gunnels
Guest
Posts: n/a
 
      01-26-2010
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?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jason Roelofs
Guest
Posts: n/a
 
      01-26-2010
[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

On Tue, Jan 26, 2010 at 1:51 PM, Chris Gunnels <(E-Mail Removed)> wrote:

> 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?
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
 
 
 
Benoit Daloze
Guest
Posts: n/a
 
      01-26-2010
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], [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 <(E-Mail Removed)>:
> 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
>
> On Tue, Jan 26, 2010 at 1:51 PM, Chris Gunnels <(E-Mail Removed)> wrote:
>
>> 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?
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>

>


 
Reply With Quote
 
Chris Gunnels
Guest
Posts: n/a
 
      01-26-2010
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/Unb...d.html#M001131

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Chris Gunnels
Guest
Posts: n/a
 
      01-26-2010
Benoit Daloze 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], [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 <(E-Mail Removed)>:


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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      01-26-2010

On Jan 26, 2010, at 13:30 , Chris Gunnels wrote:

> Benoit Daloze wrote:
>> 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], [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 <(E-Mail Removed)>:

>=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.



 
Reply With Quote
 
Roger Pack
Guest
Posts: n/a
 
      01-27-2010

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

>> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
ObjectDataSource could not find a non-generic method that has parameters ... error yet again Dennis ASP .Net 0 07-01-2006 02:25 AM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Error: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'FillMenusByLocation' that has parameters: @locationId Dotnet Gruven ASP .Net 1 09-14-2005 11:48 AM



Advertisments