Ruby Reflection Method Parameters

Discussion in 'Ruby' started by Sirisha Pusapati, Jun 17, 2009.

  1. Hi,

    I have a class called ClassFromString and it has a method called
    getCounterValue. It takes a parameter called name. Is there in Ruby
    using reflection to get the name of the parameters of a method.

    class ClassFromString
    @@counter = 0
    def initialize
    @@counter += 1
    end

    def getCounterValue(name)
    puts name
    end
    end

    def createClassFromString(classname)
    ObjectSpace.each_object(Class) do |x|
    if x.name == classname
    object = x.new
    for method in object.public_methods(false)=> Here I want to get the
    parameters that are supported by the function
    object.send(method,"Sirisha hi htere")
    end
    end

    end
    end
    createClassFromString("ClassFromString")
    --
    Posted via http://www.ruby-forum.com/.
     
    Sirisha Pusapati, Jun 17, 2009
    #1
    1. Advertising

  2. Sirisha Pusapati

    Phrogz Guest

    On Jun 16, 5:13 pm, Sirisha Pusapati <>
    wrote:
    > I have a class called ClassFromString and it has a method called
    > getCounterValue. It takes a parameter called name. Is there in Ruby
    > using reflection to get the name of the parameters of a method.


    There is not. The closest you can get is a hack that reads in source
    files (if you know where they are, and they're pure Ruby) and either
    parses the raw code or looks at the abstract syntax tree.

    Though I'm not actually recommending that you use it, this is why I
    wrote my own DescribeMethods module:
    http://phrogz.net/RubyLibs/rdoc/files/DescribeMethods_rb.html
    (Seriously way more work than you want to do, I'm sure.)
     
    Phrogz, Jun 17, 2009
    #2
    1. Advertising

  3. Phrogz wrote:
    > On Jun 16, 5:13 pm, Sirisha Pusapati <>
    > wrote:
    >> I have a class called ClassFromString and it has a method called
    >> getCounterValue. It takes a parameter called name. Is there in Ruby
    >> using reflection to get the name of the parameters of a method.

    > There is not.


    <bill-clinton>That depends on what your definition of "is"
    is.</bill-clinton>

    You are wrong, in so far as Proc#parameters does exist in Ruby 1.9.2.
    You are right, in so far as Ruby 1.9.2 doesn't exist (yet).

    See: <https://GitHub.Com/RubySpec/MatzRuby/blob/trunk/proc.c#L694-710>

    > The closest you can get is a hack that reads in source
    > files (if you know where they are, and they're pure Ruby


    .... and they exist at all (i.e. the method was not created using eval
    or define_method)

    > ) and either
    > parses the raw code or looks at the abstract syntax tree.


    Here is how the Merb-Action-Args Plugin for Merb does it:

    JRuby: <https://GitHub.Com/WyCats/Merb/blob/master/merb-action-args/lib/merb-action-args/jruby_args.rb>
    (using JRuby's own runtime reflection)

    MRI: <https://GitHub.Com/WyCats/Merb/blob/master/merb-action-args/lib/merb-action-args/mri_args.rb>
    (using ParseTree)

    For all others, it tries to use the MethoPara Gem, which, however,
    only works on YARV 1.9.1 (and *not* on Ruby 1.9.1, as the README
    incorrectly claims!): <https://GitHub.Com/Genki/MethoPara/>

    jwm
     
    Jörg W Mittag, Jun 17, 2009
    #3
    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. jerry051
    Replies:
    2
    Views:
    786
    Oliver Sturm
    Aug 2, 2005
  2. frank
    Replies:
    3
    Views:
    1,482
    Roedy Green
    Aug 20, 2005
  3. George  Moschovitis

    Method parameters reflection

    George Moschovitis, Feb 28, 2005, in forum: Ruby
    Replies:
    4
    Views:
    131
    Tim Sutherland
    Mar 6, 2005
  4. Ezra Zygmuntowicz
    Replies:
    2
    Views:
    121
    Ezra Zygmuntowicz
    Jul 10, 2006
  5. Laup-Dawg
    Replies:
    5
    Views:
    463
    John Feminella
    Feb 18, 2011
Loading...

Share This Page