# Ruby Reflection Method Parameters

**URL:** <https://www.ruby-forum.com/t/ruby-reflection-method-parameters/170394>\
**Category:** Ruby\
**Created:** [June 17, 2009, 1:12am UTC](https://www.ruby-forum.com/t/ruby-reflection-method-parameters/170394 "2009-06-17T01:12:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sirisha](https://www.ruby-forum.com/u/sirisha)\
**Post date:** [June 17, 2009, 1:12am UTC](https://www.ruby-forum.com/t/ruby-reflection-method-parameters/170394/1 "2009-06-17T01:12:59Z")

</div>

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”)

---

<div class="post-metadata">

**Author:** ![sirisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sirisha](https://www.ruby-forum.com/u/sirisha)\
**Post date:** [June 17, 2009, 2:42am UTC](https://www.ruby-forum.com/t/ruby-reflection-method-parameters/170394/2 "2009-06-17T02:42:05Z")

</div>

On Jun 16, 5:13 pm, Sirisha P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://phrogz.net/RubyLibs/rdoc/files/DescribeMethods_rb.html)  
(Seriously way more work than you want to do, I’m sure.)

---

<div class="post-metadata">

**Author:** ![sirisha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sirisha](https://www.ruby-forum.com/u/sirisha)\
**Post date:** [June 17, 2009, 10:51am UTC](https://www.ruby-forum.com/t/ruby-reflection-method-parameters/170394/3 "2009-06-17T10:51:08Z")

</div>

Phrogz wrote:

> On Jun 16, 5:13 pm, Sirisha P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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.

That depends on what your definition of “is”  
is.

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](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](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](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/](https://GitHub.Com/Genki/MethoPara/)

jwm
