Find the parameters of each method in a class

Discussion in 'Ruby' started by Chris Gunnels, Jan 26, 2010.

  1. 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/.
     
    Chris Gunnels, Jan 26, 2010
    #1
    1. Advertising

  2. [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 <> 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/.
    >
    >
     
    Jason Roelofs, Jan 26, 2010
    #2
    1. Advertising

  3. 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 <>:
    > 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 <> 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/.
    >>
    >>

    >
     
    Benoit Daloze, Jan 26, 2010
    #3
  4. Chris Gunnels, Jan 26, 2010
    #4
  5. 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], [: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 <>:


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

    --
    Posted via http://www.ruby-forum.com/.
     
    Chris Gunnels, Jan 26, 2010
    #5
  6. Chris Gunnels

    Ryan Davis Guest

    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], [: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 <>:

    >=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.
     
    Ryan Davis, Jan 26, 2010
    #6
  7. Chris Gunnels

    Roger Pack Guest


    >> 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:

    >> 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/.
     
    Roger Pack, Jan 27, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    420
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    440
    Tjerk Wolterink
    Nov 3, 2004
  3. Pat Maddox
    Replies:
    6
    Views:
    160
    Marcin Mielżyński
    Jan 20, 2006
  4. Igor Nn
    Replies:
    7
    Views:
    444
    Johnny Morrice
    May 28, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    146
    PerlFAQ Server
    Jan 26, 2011
Loading...

Share This Page