Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get argument names using reflection

Tags:

I would like to do some fairly heavy-duty reflection in Ruby. I want to create a function that returns the names of the arguments of various calling functions higher up the call stack (just one higher would be enough but why stop there?). I could use Kernel.caller, go to the file and parse the argument list but that would be ugly and unreliable.

The function that I would like would work in the following way:

module A   def method1( tuti, fruity)      foo   end   def method2(bim, bam, boom)       foo   end   def foo     print caller_args[1].join(",") #the "1" mean one step up the call stack   end  end  A.method1  #prints "tuti,fruity" A.method2  #prints "bim, bam, boom" 

I would not mind using ParseTree or some similar tool for this task but looking at Parsetree, it is not obvious how to use it for this purpose. Creating a C extension like this is another possibility but it would be nice if someone had already done it for me.

I can see that I'll probably need some kind of C extension. I suppose that means my question is what combination of C extension would work most easily. I don't think caller+ParseTree would be enough by themselves.

As far as why I would like to do this goes, rather than saying "automatic debugging", perhaps I should say that I would like to use this functionality to do automatic checking of the calling and return conditions of functions:

def add x, y    check_positive   return x + y end 

Where check_positive would throw an exception if x and y weren't positive. Obviously, there would be more to it than that but hopefully this gives enough motivation.

like image 272
Joe Soul-bringer Avatar asked Mar 07 '09 19:03

Joe Soul-bringer


People also ask

Can I obtain method parameter name using Java reflection?

You can obtain the names of the formal parameters of any method or constructor with the method java. lang. reflect.

How do you find the class name in reflection?

Getting class name using reflection If you want to get the class name using the instance of the Class. As you can see using the method getName() gives you the fully qualified name whereas getSimpleName() method gives you only the class name.

What method can be used to read parameter names?

Following is a generic example which uses getParameterNames() method of HttpServletRequest to read all the available form parameters. This method returns an Enumeration that contains the parameter names in an unspecified order.

Which method is used to fetch the parameter types using method parameter reflection?

reflect package is used to fetch the parameter types using method parameter reflection. Reflection is a process of analyzing and modifying all capabilities of class at runtime.


2 Answers

In Ruby 1.9.2, you can trivially get the parameter list of any Proc (and thus of course also of any Method or UnboundMethod) with Proc#parameters:

A.instance_method(:method1).parameters # => [[:req, :tuti], [:req, :fruity]] 

The format is an array of pairs of symbols: type (required, optional, rest, block) and name.

For the format you want, try

A.instance_method(:method1).parameters.map(&:last).map(&:to_s)   # => ['tuti', 'fruity'] 

Of course, that still doesn't give you access to the caller, though.

like image 65
Jörg W Mittag Avatar answered Oct 14 '22 10:10

Jörg W Mittag


I suggest you take a look at Merb's action-args library.

require 'rubygems' require 'merb'  include GetArgs  def foo(bar, zed=42) end  method(:foo).get_args # => [[[:bar], [:zed, 42]], [:zed]] 

If you don't want to depend on Merb, you can choose and pick the best parts from the source code in github.

like image 22
Antti Tarvainen Avatar answered Oct 14 '22 11:10

Antti Tarvainen